国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

實例講解Android中SQLiteDatabase使用方法

2019-12-12 06:20:05
字體:
來源:轉載
供稿:網友

SQLite數據庫是android系統內嵌的數據庫,小巧強大,能夠滿足大多數SQL語句的處理工作,而SQLite數據庫僅僅是個文件而已。雖然SQLite的有點很多,但并不是如同PC端的mysql般強大,而且android系統中不允許通過JDBC操作遠程數據庫,所以只能通過webservice等手段于php、servlet交互獲取數據。

SQLiteDatabase類,代表了一個數據庫對象,通過SQLiteDatabase來操作管理數據庫。

一些基本的用法:

  static  SQLiteDatabase openDatabase(String path,SQLiteDatabase.CUrsorFactory factory,int flag);

  static SQLiteDatabase openOrCreateDatabase(File file,SQLiteDatabase.CursorFactory factory);

  static SQLiteDatabase openOrCreateDatabase(String path,SQLiteDatabse.CursorFactory factory);

通過這些靜態方法可以很方便的打開和新建一個數據庫。

1、execSQL(String sql,Object[] bindArgs)

2、execSQL(String sql)

3、rawQuery(String sql,String[] selectionArgs);

4、beginTransaction()

5、endTransaction()

這些函數可以完成SQL功能,對于查詢出來的結果是用Cursor表示的,類似于JDBC中的ResultSet類,在這些類中通過方法move(int offset)、moveToFirst()、moveToLast()、moveToNext()、moveToPosition(int position)、moveToPrivious()獲取需要的結果行。

下面通過一個實例來說明一下SQLiteDatabase的基本使用

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context=".Main" >  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:gravity="center"      android:text="key" />    <EditText      android:id="@+id/keys"      android:layout_width="100sp"      android:layout_height="wrap_content" />    <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:gravity="center"      android:text="value" />    <EditText      android:id="@+id/values"      android:layout_width="100sp"      android:layout_height="wrap_content" />    <Button      android:id="@+id/btn"      android:layout_width="100sp"      android:layout_height="wrap_content"      android:text="submit" />  </LinearLayout>  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content" >    <ListView      android:id="@+id/lv"      android:layout_width="match_parent"      android:layout_height="wrap_content" />  </LinearLayout></LinearLayout>

用于填充數據的mytextview.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:orientation="horizontal" >  <TextView    android:id="@+id/listkey"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="left" />  <TextView    android:id="@+id/listvalue"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="300sp" /></LinearLayout>

Main.java

package com.app.main;import android.annotation.SuppressLint;import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CursorAdapter;import android.widget.EditText;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class Main extends Activity {  EditText ed1 = null;  EditText ed2 = null;  Button btn = null;  ListView lv = null;  SQLiteDatabase db = null;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    ed1 = (EditText) this.findViewById(R.id.keys);    ed2 = (EditText) this.findViewById(R.id.values);    btn = (Button) this.findViewById(R.id.btn);    lv = (ListView) this.findViewById(R.id.lv);    db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()        + "/my.db3", null);    btn.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View view) {        String key = ed1.getText().toString();        String value = ed2.getText().toString();        try {          insertData(db, key, value);          Cursor cursor = db.rawQuery("select * from tb_info", null);          inflateListView(cursor);        } catch (Exception e) {          String sql = "create table tb_info(_id integer primary key autoincrement,db_key varchar(20),db_value varchar(50))";          db.execSQL(sql);          insertData(db, key, value);          Cursor cursor = db.rawQuery("select * from tb_info", null);          inflateListView(cursor);        }      }    });  }  // 向數據庫中插入數據  private void insertData(SQLiteDatabase db, String key, String value) {    db.execSQL("insert into tb_info values (null,?,?)", new String[] { key,        value });    System.out.println("------------------");  }  // 向ListView中填充數據  @SuppressLint("NewApi")  public void inflateListView(Cursor cursor) {    SimpleCursorAdapter adapter = new SimpleCursorAdapter(Main.this,        R.layout.mytextview, cursor, new String[] { "db_key",            "db_value" },        new int[] { R.id.listkey, R.id.listvalue },        CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);    lv.setAdapter(adapter);  }  @Override  protected void onDestroy() {    super.onDestroy();    if (db != null && db.isOpen()) {      db.close();    }  }}

實現的效果:

需要特別指出,在用SimpleCursorAdapter封裝Cursor的時候,要求底層數據庫表的主鍵列的列名為_id,因為SimpleCursorAdapter只能識別主鍵列名為_id的表。

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博爱县| 曲阳县| 循化| 龙胜| 华安县| 佛坪县| 吉木乃县| 顺义区| 九龙坡区| 顺义区| 洪江市| 北辰区| 闸北区| 陵水| 大悟县| 磐安县| 嘉禾县| 赤峰市| 石城县| 湟中县| 吕梁市| 金坛市| 民权县| 涟源市| 双辽市| 宁德市| 固始县| 凭祥市| 商都县| 扎赉特旗| 启东市| 荣昌县| 象山县| 普兰店市| 天等县| 金沙县| 顺义区| 河间市| 永年县| 长宁县| 鄂州市|