溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

通過SQL語句查詢需要的數(shù)據(jù)

發(fā)布時間:2021-09-04 18:36:02 來源:億速云 閱讀:132 作者:chen 欄目:數(shù)據(jù)庫

這篇文章主要講解了“通過SQL語句查詢需要的數(shù)據(jù)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“通過SQL語句查詢需要的數(shù)據(jù)”吧!

通過SQL語句查詢需要的數(shù)據(jù)

在main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/mylayout"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:gravity="center_horizontal">

    <Button

        android:id="@+id/findBut"

        android:layout_marginTop="8dp"

        android:background="#0066ff"

        android:textColor="#ffffff"

        android:layout_width="100dp"

        android:layout_height="40dp"

        android:text="查詢?nèi)繑?shù)據(jù)" />

</LinearLayout>

在MyDatabaseHelper.java類中:

package com.li.sqlite;

//數(shù)據(jù)庫的輔助操作類

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {

  private static final String DATABASENAME = "liyewen.db" ;

  private static final int DATABASERVERSION = 1 ;  // 設(shè)置數(shù)據(jù)庫的版本

  private static final String TABLENAME = "mytab" ;

  public MyDatabaseHelper(Context context) {  // 用戶最關(guān)心的也肯定只是Context

     super(context, DATABASENAME, null, DATABASERVERSION);

  }

  @Override

  public void onCreate(SQLiteDatabase db) { // 創(chuàng)建數(shù)據(jù)表

     String sql = "CREATE TABLE " + TABLENAME + "("

         + "id    INTEGER       PRIMARY KEY ,"   // 在SQLite中設(shè)置為Integer、PRIMARY KEY則ID自動增長

         + "name   VARCHAR(50)   NOT NULL ,"

         + "birthday DATE NOT    NULL" + ")";

     db.execSQL(sql) ;  // 執(zhí)行SQL

     System.out.println("****************** 創(chuàng)建:onCreate()。");

  }

  @Override

  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

     String sql = "DROP TABLE IF EXISTS " + TABLENAME ;

     db.execSQL(sql) ;

     System.out.println("****************** 更新:onUpgrade()。");

     this.onCreate(db) ;

  }

}

在MytabCursor.java類中:

package com.li.sqlite;

import java.util.ArrayList;

import java.util.List;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

public class MytabCursor {

  private static final String TABLENAME = "mytab" ;

  private SQLiteDatabase db = null ;

  public MytabCursor(SQLiteDatabase db) {

     this.db = db ;

  }

  public List<String> find(){

     List<String> all = new ArrayList<String>() ; // 此時只是String

     String sql = "SELECT id,name,birthday FROM " + TABLENAME ;

     Cursor result = this.db.rawQuery(sql, null); // 執(zhí)行查詢語句

     for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) { // 采用循環(huán)的方式檢索數(shù)據(jù)

       all.add("【" + result.getInt(0) + "】" + " " + result.getString(1)

            + "," + result.getString(2));

     }

     this.db.close() ;

     return all ;

  }

}

MySQLiteDemo.java中:

package com.li.sqlite;

import android.app.Activity;

import android.database.sqlite.SQLiteOpenHelper;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.ListView;

public class MySQLiteDemo extends Activity {

  private Button findBut = null;

  private SQLiteOpenHelper helper = null;

  private LinearLayout mylayout = null;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.findBut = (Button)super.findViewById(R.id.findBut);

     this.mylayout = (LinearLayout)super.findViewById(R.id.mylayout);

     this.findBut.setOnClickListener(new OnClickListenerImpl());

  }

  private class OnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MySQLiteDemo.this.helper = new MyDatabaseHelper(MySQLiteDemo.this);

       ListView listView = new ListView(MySQLiteDemo.this);

       listView.setAdapter( //設(shè)置數(shù)據(jù)

            new ArrayAdapter<String>  //所有的數(shù)據(jù)是字符串

            (MySQLiteDemo.this,       //上下文

            android.R.layout.simple_list_item_1, //列表顯示的布局

            new MytabCursor(     //實(shí)例化查詢

            MySQLiteDemo.this.helper.getReadableDatabase())  //取得SQLiteDatabase對象

            .find()));    //調(diào)用find()方法,返回List<String>

       MySQLiteDemo.this.mylayout.addView(listView);

     }

  }

}

感謝各位的閱讀,以上就是“通過SQL語句查詢需要的數(shù)據(jù)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對通過SQL語句查詢需要的數(shù)據(jù)這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

sql
AI