編輯:關於 Android 程式設計
本文通過一個完整示例,演示如何在 Android 中使用 SQLiteOpenHelper 封裝數據庫操作,並實現數據庫的創建、升級、插入、修改與查詢。
界面由五個按鈕組成,分別對應創建、更新、插入、修改、查詢等操作:
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>
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 }
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 游標遍歷讀取。
微信未讀消息截圖怎麼製作?趣味聊天畫面生成教學
你是否看過朋友分享那種滿屏未讀訊息的微信聊天截圖?其實這種趣味畫面是可以自己製作的。透過線上小工具,你能自訂頭像、暱稱與訊息內容,讓聊天介面顯示大量未讀訊息,截圖分享給朋
Android 內部儲存讀寫檔案實作教學
實務開發裡,「把一個檔案寫進手機」幾乎是必備技能。要談寫檔,先把儲存空間的輪廓交代清楚。 零、儲存空間的分類 內部儲存空間 • RAM:執行用的記憶體,等同電腦
Android 圖表實作:折線圖、柱狀圖與餅圖
效果如下圖:本文以 achartengine 為基礎,示範如何在 Android 中繪製折線圖、柱狀圖與餅圖三種常見圖表。1. MainActivity.java pac
Android 動態修改 RelativeLayout 寬高的方法
在 Android 開發中,常需要依資料或互動狀態動態改變 RelativeLayout 的寬高。最常見的做法是重新建立 LayoutParams,但如果使用不當,可能會