先上一张效果图:

每次点击“插入”,都会在下面的ListView中新增一行
本文涉及四个文件,分别是DBTest.java、main.xml、line.xml、strings.ml
01 | public class DBTest extends Activity { |
07 | protected void onCreate(Bundle savedInstanceState) { |
08 | super .onCreate(savedInstanceState); |
09 | setContentView(R.layout.main); |
11 | Log.d( "myLog" , this .getFilesDir().toString()); |
13 | db = SQLiteDatabase.openOrCreateDatabase( this .getFilesDir() |
14 | .toString() + "/my.db3" , null ); |
15 | listView = (ListView) findViewById(R.id.show); |
16 | btn = (Button) findViewById(R.id.ok); |
17 | btn.setOnClickListener( new OnClickListener() { |
20 | public void onClick(View source) { |
22 | String title = ((EditText)findViewById(R.id.title)) |
23 | .getText().toString(); |
24 | String content = ((EditText)findViewById(R.id.content)) |
25 | .getText().toString(); |
28 | insertData(db, title, content); |
29 | Cursor cursor = db.rawQuery( "select * from news_inf" , null ); |
31 | } catch (SQLiteException se){ |
33 | db.execSQL( "create table news_inf(_id integer primary key autoincrement," |
34 | + " news_title varchar(50)," |
35 | + " news_content varchar(255))" ); |
37 | insertData(db, title, content); |
39 | Cursor cursor = db.rawQuery( "select * from news_inf" , null ); |
47 | private void insertData(SQLiteDatabase db |
48 | , String title , String content){ |
50 | db.execSQL( "insert into news_inf values(null , ? , ?)" |
51 | , new String[]{title , content}); |
54 | private void inflateList(Cursor cursor){ |
56 | SimpleCursorAdapter adapter = new SimpleCursorAdapter( |
57 | DBTest. this , R.layout.line, cursor |
58 | , new String[]{ "news_title" , "news_content" } |
59 | , new int []{R.id.my_title, R.id.my_content}); |
61 | listView.setAdapter(adapter); |
65 | protected void onDestroy() { |
68 | if (db != null && db.isOpen()){ |
其中db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3", null);用于创建或打开SQLite数据库。当点击按钮的时候,程序会调用insertData方法,向底层数据库表中插入一行记录。然后再执行查询语句,把底层数据表中的记录查询出来,并使用ListView将查询结果(Cursor)显示出来。
1 | SimpleCursorAdapter adapter = new SimpleCursorAdapter( |
2 | DBTest. this , R.layout.line, cursor |
3 | , new String[]{ "news_title" , "news_content" } |
4 | , new int []{R.id.my_title, R.id.my_content}); |
以上代码用于将Cursor封装成SimpleCursorAdapter,这个SimpleCursorAdapter实现了Adapter接口,可以作为ListView的内容适配器。Cursor里的每一行可以当成Map处理(以数据列的列名为key,数据列的值为value)。SimpleCursorAdapter这里有5个参数DBTest.this就是当前文件,R.layout.line是line.xml布局文件,cursor是执行完SQl语句之后,获取的游标,第4个参数是from,第5个参数是to,可以理解为将数据库里的news_title、news_content的字段值赋给line.xml里面的my_title、my_content。
main.xml
01 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
02 | xmlns:tools = "http://schemas.android.com/tools" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
06 | tools:context = ".DBTest" > |
09 | android:id = "@+id/title" |
10 | android:layout_width = "fill_parent" |
11 | android:layout_height = "wrap_content" |
14 | android:id = "@+id/content" |
15 | android:layout_width = "fill_parent" |
16 | android:layout_height = "wrap_content" |
20 | android:layout_width = "wrap_content" |
21 | android:layout_height = "wrap_content" |
22 | android:text = "@string/insert" |
25 | android:id = "@+id/show" |
26 | android:layout_width = "fill_parent" |
27 | android:layout_height = "fill_parent" |
line.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "horizontal" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
08 | android:id = "@+id/my_title" |
09 | android:layout_width = "wrap_content" |
10 | android:layout_height = "wrap_content" |
14 | android:id = "@+id/my_content" |
15 | android:layout_width = "fill_parent" |
16 | android:layout_height = "wrap_content" |
strings.xml中加入下面一行 ,一般按钮、TextView的文字都写在这个文件中,这样方便维护。比如说需要修改某些字,或者提供不同的语言版本的时候,可以直接在里面改。当然有些语言开发者不熟悉的时候,可以直接把整个文件给有能力翻译的人,这样非常方便。
1 | < string name = "insert" >插入</ string > |
总结使用SQLiteDatabase进行数据库操作的步骤:
1.获取SQLiteDatabase对象,它代表了与数据库的连接
2.调用SQLiteDatabase的方法来执行SQL语句
3.操作SQL语句的执行效果,比如用SimpleCursorAdapter封装Cursor
4.关闭SQLiteDatabase,回收资源