使用SQL语句操作SQLite数据库

来源:互联网 发布:c gui qt4编程 编辑:程序博客网 时间:2024/06/11 15:23

先上一张效果图:


每次点击“插入”,都会在下面的ListView中新增一行


本文涉及四个文件,分别是DBTest.java、main.xml、line.xml、strings.ml

01public class DBTest extends Activity {
02 
03    SQLiteDatabase db;
04    Button btn = null;
05    ListView listView;
06    @Override
07    protected void onCreate(Bundle savedInstanceState) {
08        super.onCreate(savedInstanceState);
09        setContentView(R.layout.main);
10        //获取路径,也算是一种调试的方法吧
11        Log.d("myLog"this.getFilesDir().toString());
12//创建或打开数据库(此处需要使用据对路径)
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() {
18             
19            @Override
20            public void onClick(View source) {
21                //获取用户输入
22                String title = ((EditText)findViewById(R.id.title))
23                        .getText().toString();
24                String content = ((EditText)findViewById(R.id.content))
25                        .getText().toString();
26//如果没有数据库表就执行catch语句,先创建表,再执行其他语句
27                try{
28                    insertData(db, title, content);
29                    Cursor cursor = db.rawQuery("select * from news_inf"null);
30                    inflateList(cursor);
31                }catch(SQLiteException se){
32                    //执行DDL创建数据表
33                    db.execSQL("create table news_inf(_id integer primary key autoincrement,"
34                            +" news_title varchar(50),"
35                            +" news_content varchar(255))");
36                    //执行insert语句插入数据
37                    insertData(db, title, content);
38                    //执行查询
39                    Cursor cursor = db.rawQuery("select * from news_inf"null);
40                    inflateList(cursor);
41                }
42            }
43        });
44    }
45 
46     
47    private void insertData(SQLiteDatabase db
48            , String title , String content){
49        //执行插入语句
50        db.execSQL("insert into news_inf values(null , ? , ?)"
51                new String[]{title , content});
52    }
53     
54    private void inflateList(Cursor cursor){
55            //填充SimpleCursorAdapter
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});
60        //显示数据
61        listView.setAdapter(adapter);
62    }
63 
64    @Override
65    protected void onDestroy() {
66        super.onDestroy();
67        //退出程序时关闭SQLiteDatabase
68        if(db != null && db.isOpen()){
69            db.close();
70        }
71    }
72     
73     
74 
75}

其中db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3", null);用于创建或打开SQLite数据库。当点击按钮的时候,程序会调用insertData方法,向底层数据库表中插入一行记录。然后再执行查询语句,把底层数据表中的记录查询出来,并使用ListView将查询结果(Cursor)显示出来。

1SimpleCursorAdapter 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" >
07 
08    <EditText
09        android:id="@+id/title"
10        android:layout_width="fill_parent"
11        android:layout_height="wrap_content"
12        />
13    <EditText
14        android:id="@+id/content"
15        android:layout_width="fill_parent"
16        android:layout_height="wrap_content"
17        android:lines="2"/>
18    <Button
19        android:id="@+id/ok"
20        android:layout_width="wrap_content"
21        android:layout_height="wrap_content"
22        android:text="@string/insert"
23        />
24    <ListView
25        android:id="@+id/show"
26        android:layout_width="fill_parent"
27        android:layout_height="fill_parent"
28        />
29</LinearLayout>

 

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"
06    >
07<EditText
08    android:id="@+id/my_title"
09    android:layout_width="wrap_content"
10    android:layout_height="wrap_content"
11    android:width="120px"
12    />
13<EditText 
14    android:id="@+id/my_content"
15    android:layout_width="fill_parent"
16    android:layout_height="wrap_content"
17    />  
18</LinearLayout>

 

strings.xml中加入下面一行 ,一般按钮、TextView的文字都写在这个文件中,这样方便维护。比如说需要修改某些字,或者提供不同的语言版本的时候,可以直接在里面改。当然有些语言开发者不熟悉的时候,可以直接把整个文件给有能力翻译的人,这样非常方便。

1<string name="insert">插入</string>

 

总结使用SQLiteDatabase进行数据库操作的步骤:

1.获取SQLiteDatabase对象,它代表了与数据库的连接

2.调用SQLiteDatabase的方法来执行SQL语句

3.操作SQL语句的执行效果,比如用SimpleCursorAdapter封装Cursor

4.关闭SQLiteDatabase,回收资源

原创粉丝点击