Android学习笔记-SQLite的使用

wKioL1RsXxnz29SwAAC3SlphABw567.jpg

界面文件activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    <Button 
        android:id="@+id/createButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/hello_world"
        android:text="create database"/>
 
    <Button 
        android:id="@+id/updateButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/createButton"
        android:text="update database"/>
     
    <Button 
        android:id="@+id/insertButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/updateButton"
        android:text="insert database"/>
     
    <Button 
        android:id="@+id/updateRecordButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/insertButton"
        android:text="update record"/>
     
    <Button 
        android:id="@+id/queryRecordButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/updateRecordButton"
        android:text="Query record"/>

DatabaseHelper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.sqlite_01;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
 
public class DatabaseHelper extends SQLiteOpenHelper{
 
    private static final int VERSION = 1;
     
    public DatabaseHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }
     
    public DatabaseHelper(Context context, String name, int  version) {
        this(context, name, null, version);
    }
     
    public DatabaseHelper(Context context, String name) {
        this(context, name, VERSION);
    }
 
    //第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabase对象的时候调用
    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("Create a database");
        db.execSQL("create table user(id int, name varchar(20))");
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("Update a database");
    }
 
    /*
     * abd shell进入当前App的目录下的databases目录下就可以看到创建的数据库
     * 打开数据库sqlite3 test_db
     * 查看当前数据库里的表以及创建表的语句 .schema
     */
}

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.example.sqlite_01;
 
import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends ActionBarActivity {
     
    private Button createButton = null;
    private Button updateButton = null;
    private Button insertButton = null;
    private Button updateRecordButton = null;
    private Button queryRecordButton = null;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        createButton = (Button) findViewById(R.id.createButton);
        updateButton = (Button) findViewById(R.id.updateButton);
        insertButton = (Button) findViewById(R.id.insertButton);
        updateRecordButton = (Button) findViewById(R.id.updateRecordButton);
        queryRecordButton = (Button) findViewById(R.id.queryRecordButton);
         
        createButton.setOnClickListener(new CreateListener());
        updateButton.setOnClickListener(new UpdateListener());
        insertButton.setOnClickListener(new InsertListener());
        updateRecordButton.setOnClickListener(new UpdateRecordListener());
        queryRecordButton.setOnClickListener(new QueryListener());
    }
 
 
    class CreateListener implements OnClickListener{
 
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this"test_db");
            SQLiteDatabase db = dbHelper.getReadableDatabase();//执行了这一句才会创建数据库
        }
    }
     
    class UpdateListener implements OnClickListener{
 
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this"test_db"2);
            SQLiteDatabase db = dbHelper.getReadableDatabase();//执行了这一句才会创建数据库
        }
    }
     
    class InsertListener implements OnClickListener{
 
        @Override
        public void onClick(View v) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("id"1);
            contentValues.put("name""umgsai");
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this"test_db"2);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            db.insert("user"null, contentValues);
        }
    }
     
    class UpdateRecordListener implements OnClickListener{
 
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this"test_db"2);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name""admin");
            db.update("user", values, "id = ?"new String[]{"1"});
        }
    }
     
    class QueryListener implements OnClickListener{
 
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this"test_db"2);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            //db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
            Cursor cursor = db.query("user"new String[]{"id""name"}, "id = ?"new String[]{"1"}, nullnull"id");
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                System.out.println("name>>>" + name);
            }
        }
         
    }
}



可以通过adb shell命令来查看数据库

>abd shell    

>ls -l        (查看当前目录下的文件及文件夹)

>cd data  (进入data目录)

>ls -l        (查看当前目录下的文件及文件夹)

>cd data  (进入data目录)

>ls -l        (查看当前目录下的文件及文件夹)

>cd com.example.sqlite_01  (进入当前项目目录)

>ls -l        (查看当前目录下的文件及文件夹)

>cd databases  (进入databases目录)

>ls -l        (此时就可以看到数据库文件test_db)

>sqlite3 test_db  (使用sqlite打开test_db数据库)

>.schema  (查看当前数据库中的表以及建表的语句)

>select * from user;  (从user表中查询数据)

posted on 2014-11-19 17:25 ^小^齐^ 阅读(3785) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

相册

我喜欢去的地方

搜索

最新评论

阅读排行榜

评论排行榜