Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android SQLite 數據庫的增刪改查實戰

編輯:關於 Android 程式設計

本文通過一個完整示例,演示如何在 Android 中使用 SQLiteOpenHelper 封裝數據庫操作,並實現數據庫的創建、升級、插入、修改與查詢。

一、界面布局 main.xml

界面由五個按鈕組成,分別對應創建、更新、插入、修改、查詢等操作:

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

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

03     android:orientation="vertical"

04     android:layout_width="fill_parent"

05     android:layout_height="fill_parent"

06     >

07 <Button 

08     android:layout_width="fill_parent"

09     android:layout_height="wrap_content"

10     android:id="@+id/create"

11     android:text="創建數據庫"

12     />

13 <Button 

14     android:layout_width="fill_parent"

15     android:layout_height="wrap_content"

16     android:id="@+id/upgrade"

17     android:text="更新數據庫"

18     />

19 <Button 

20     android:layout_width="fill_parent"

21     android:layout_height="wrap_content"

22     android:id="@+id/insert"

23     android:text="插入數據"

24     />

25 <Button 

26     android:layout_width="fill_parent"

27     android:layout_height="wrap_content"

28     android:id="@+id/update"

29     android:text="修改數據"

30     />

31 <Button 

32     android:layout_width="fill_parent"

33     android:layout_height="wrap_content"

34     android:id="@+id/select"

35     android:text="查詢數據"

36     />

37 </LinearLayout>

二、繼承自 SQLiteOpenHelper 的 MyOpenHleper 類

01 package com.android.danny.hleper;

02  

03 import android.content.Context;

04 import android.database.sqlite.SQLiteDatabase;

05 import android.database.sqlite.SQLiteOpenHelper;

06  

07 public class MyOpenHleper extends SQLiteOpenHelper {

08  

09     public static final int VERSION =1;

10      

11     public MyOpenHleper(Context context, String name) {

12         super(context, name, null, VERSION);

13         // TODO Auto-generated constructor stub

14     }

15      

16     public MyOpenHleper(Context context, String name,int ver) {

17         super(context, name, null, ver);

18         // TODO Auto-generated constructor stub

19     }

20     @Override

21     public void onCreate(SQLiteDatabase db) {

22         System.out.println("------Create Database----");

23         db.execSQL("CREATE TABLE person (id int ,name varchar(20))");

24     }

25  

26     @Override

27     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

28         // TODO Auto-generated method stub

29         System.out.println("------Upgrade Database----");

30     }

31  

32 }

三、SQLite 數據操作類

01 package com.android.danny.db;

02  

03 import com.android.danny.hleper.MyOpenHleper;

04  

05 import android.app.Activity;

06 import android.content.ContentValues;

07 import android.database.Cursor;

08 import android.database.sqlite.SQLiteDatabase;

09 import android.os.Bundle;

10 import android.view.View;

11 import android.view.View.OnClickListener;

12 import android.widget.Button;

13  

14 public class Sqlite3 extends Activity implements OnClickListener{

15      

16     Button create;

17     Button upgrade;

18     Button insert;

19     Button update;

20     Button select;

21      

22     @Override

23     public void onCreate(Bundle savedInstanceState) {

24         super.onCreate(savedInstanceState);

25         setContentView(R.layout.main);

26          

27         create = (Button)findViewById(R.id.create);

28         upgrade = (Button)findViewById(R.id.upgrade);

29         insert = (Button)findViewById(R.id.insert);

30         update = (Button)findViewById(R.id.update);

31         select = (Button)findViewById(R.id.select);

32          

33         create.setOnClickListener(this);

34         upgrade.setOnClickListener(this);

35         insert.setOnClickListener(this);

36         update.setOnClickListener(this);

37         select.setOnClickListener(this);

38     }

39  

40     @Override

41     public void onClick(View v) {

42         MyOpenHleper dbDatabase;

43         if (v == create) {

44             dbDatabase = new MyOpenHleper(this, "test_db.db");

45             //創建數據庫

46             dbDatabase.getWritableDatabase();

47              

48         }else if (v == upgrade) {

49             //更新數據庫

50             dbDatabase = new MyOpenHleper(this, "test_db.db",2);

51             dbDatabase.getReadableDatabase();

52              

53         }else if (v == insert) {

54             //插入數據

55             ContentValues values = new ContentValues();

56             values.put("id", 1);

57             values.put("name", "danny");

58             dbDatabase = new MyOpenHleper(this, "test_db.db");

59             SQLiteDatabase database = dbDatabase.getWritableDatabase();

60             database.insert("person", null, values);

61              

62         }else if (v == update) {

63             //修改數據

64             ContentValues values = new ContentValues();

65             values.put("name", "danny&kiki");

66             dbDatabase = new MyOpenHleper(this, "test_db.db");

67             SQLiteDatabase database = dbDatabase.getWritableDatabase();

68             database.update("person", values, "id=?", new String[] {"1"});

69              

70         }else if (v == select) {

71             //數據查詢操作

72             dbDatabase = new MyOpenHleper(this, "test_db.db");

73             SQLiteDatabase database = dbDatabase.getReadableDatabase();

74             Cursor cursor = database.query("person", new String[] {"id","name"},

75                     "id=?", new String[] {"1"},

76                     null, null, null);

77             while (cursor.moveToNext()) {

78                     String name = cursor.getString(cursor.getColumnIndex("name"));

79                     System.out.println("query:----->"+name);

80             }

81         }

82          

83     }

84 }

通過這個示例可以看到:數據庫的創建與升級由 onCreate 和 onUpgrade 處理,而插入、修改、查詢則分別調用 insert、update、query 方法完成,查詢結果通過 Cursor 游標遍歷讀取。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved