/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:数据库应用—访问数据表(SQLite OpenHelper)
* 作 者: 雷恒鑫
* 完成日期: 2012 年 08 月 14 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
在“DummyNote”项目的“src/com/demo/android/dummynote”目录下新建一个“NotesDbAdapter.java”文件,程序的主框架如下:
package com.demo.android.dummynote; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class NotesDbAdapter{ private static final String DATABASE_NAME = "notes.db"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_TABLE = "notes"; private static final String DATABASE_CREATE = "creat table notes(" +"_id INTEGER PRIMARY KEY," +"note TEXT," +"created INTEGER," +"modified INTEGER" +");"; private static class DatabaseHelper extends SQLiteOpenHelper{ public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE); onCreate(db); } } }
对“NotesDbAdapter”类进行加工:
在“NotesDbAdapter.java”文件里继续添加代码:
package com.demo.android.dummynote; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class NotesDbAdapter{ private static final String DATABASE_NAME = "notes.db"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_TABLE = "notes"; private static final String DATABASE_CREATE = "creat table notes(" +"_id INTEGER PRIMARY KEY," +"note TEXT," +"created INTEGER," +"modified INTEGER" +");"; private static class DatabaseHelper extends SQLiteOpenHelper{ public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE); onCreate(db); } } private Context mCtx = null; private DatabaseHelper dbHelper; private SQLiteDatabase db; public NotesDbAdapter(Context ctx){ this.mCtx = ctx; } public NotesDbAdapter open () throws SQLException { dbHelper = new DatabaseHelper(mCtx); db = dbHelper.getWritableDatabase(); return this; } public void close(){ dbHelper.close(); } }
在打开“DummyNote.java”文件,修改“setAdapter”函数如下:
private NotesDbAdapter mDbHelper; private void setAdapter(){ mDbHelper = new NotesDbAdapter(this); mDbHelper.open(); ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,note_array); setListAdapter(adapter); }
完整的“DummyNote.java”文件如下:
package com.demo.android.dummynote; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListAdapter; public class DummyNote extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Tell the list view which view to display when the list is empty getListView().setEmptyView(findViewById(R.id.empty)); setAdapter(); } private String[] note_array = { "gasolin", "crota", "louk", "magicion" }; private NotesDbAdapter mDbHelper; private void setAdapter(){ mDbHelper = new NotesDbAdapter(this); mDbHelper.open(); ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,note_array); setListAdapter(adapter); } }
验证: 使用“adb shell”命令,进入“cddata/data/com.deno.android.dummynote/databases”目录后,执行“sqlite3 notes.db”命令,查看“notes”数据表是否已经创建。
从上面的结果看,“notes”数据表以经正确创建了,我们可以通过“sqlite>”互动模式来尝试在“notes”数据表中增加几条数据:
查看新增内容是否正确(用SELECT”语句):
可以看到,数据分成3列,分别为“_id”、“note”、“created”。每个字段中确实都有填入数值。