Android的SQLite基本操作

涉及SQLite的增删改查,结果用log显示

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
package com.example.sqlconnecttest;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
public class DBHelper extends SQLiteOpenHelper{
 
    /*
     * 必须有的构造器
     */
    public DBHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }
 
    /*
     * 当第一次创建数据库时,就调用该方法
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(createDatabases, 创建数据库--->);
    }
 
    /*
     * 当更新数据库时,调用该方法
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(updateDatabase, 更新数据库--->);
    }
}

 

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.example.sqlconnecttest;
 
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity implements OnClickListener{
 
    private Button createDatabase;
    private Button createTable;
    private Button insert;
    private Button select;
    private Button update;
    private Button delete;
     
    private final String DATABASE_NAME = myDatabase;
    private SQLiteDatabase mySQLiteDatabase = null;
    private final String TABLE_NAME = user;
    private String SQL =
            CREATE TABLE [user] (+
            [id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, +
            [username] VARCHAR NOT NULL, +
            [password] VARCHAR NOT NULL, +
            [phoneNumber] VARCHAR NOT NULL);
    private DBHelper db;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createView();
        setListener();
    }
 
    private void createView(){
        createDatabase = (Button) findViewById(R.id.createDatabase);
        createTable = (Button) findViewById(R.id.createTable);
        insert = (Button) findViewById(R.id.insert);
        select = (Button) findViewById(R.id.select);
        update = (Button) findViewById(R.id.update);
        delete = (Button) findViewById(R.id.delete);
    }
    //加监听器
    private void setListener(){
        createDatabase.setOnClickListener(this);
        createTable.setOnClickListener(this);
        insert.setOnClickListener(this);
        select.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
    }
        //各种监听方法
    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.createDatabase:
        {
            db = new DBHelper(MainActivity.this,DATABASE_NAME,null,1);
            mySQLiteDatabase = db.getWritableDatabase();
            Log.i(数据库对象, mySQLiteDatabase.toString());
            //System.out.print(数据库对象+mySQLiteDatabase);
            break;
        }
        case R.id.createTable:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            try {
                mySQLiteDatabase.execSQL(SQL);
                Log.i(创建表, SQL);
            } catch (SQLException e) {
                e.printStackTrace();
                Log.i(创建表--》, 创建表失败----------------》);
            }
            break;
        }
        case R.id.insert:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(username, 老爸);
            cv.put(password, 123456);
            cv.put(phoneNumber, 134756658888);
            long n = mySQLiteDatabase.insert(TABLE_NAME, null, cv);
            Log.i(插入数据, n + );
            mySQLiteDatabase.close();
            break;
        }
        case R.id.select:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getReadableDatabase();
            /**
             * 参数1:表名
             * 参数2:要显示的列
             * 参数3:where语句
             * 参数4:where语句的条件值
             * 参数5:分组方式
             * 参数6:having条件
             * 参数7:排序方式
             */
            Cursor cursor = mySQLiteDatabase.query(TABLE_NAME,
                    new String[]{id,username,password,phoneNumber},
                    null, null, null, null, null);
            while(cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex(id));
                String username = cursor.getString(cursor.getColumnIndex(username));
                String password = cursor.getString(cursor.getColumnIndex(password));
                String phoneNumber = cursor.getString(cursor.getColumnIndex(phoneNumber));
                Log.i(query-->, id: + id + userName: + username +
                      password + password + phoneNumber + phoneNumber);
            }
            mySQLiteDatabase.close();
            break;
        }
        case R.id.update:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            ContentValues cv1 = new ContentValues();
            cv1.put(username, admin);
            cv1.put(password, admin);
            cv1.put(phoneNumber, 1388888);
            String whereClause = id + =?;
            String[] whereArgs = {8};
            /*
             * 参数1:表名
             * 参数2:是一个ContextValue对象,就是更新的值
             * 参数3:where语句条件
             * 参数4:where条件的值
             */
            int index = mySQLiteDatabase.update(TABLE_NAME, cv1, whereClause, whereArgs);
            Log.i(update-->, index + );
            break;
        }
        case R.id.delete:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            /***
             * 参数1:表名
             * 参数2:where语句字段
             * 参数3:where语句字段的值
             */
            String whereClause1 = id + =?;
            String[] whereArgs1 = {1};
            int num = mySQLiteDatabase.delete(TABLE_NAME, whereClause1, whereArgs1);
            Log.i(删除记录-->, num + );
            mySQLiteDatabase.close();
            break;
        }
        default:
            break;
        }
    }
}
1
2
3
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.sqlconnecttest.MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
 
    <textview android:id="@+id/textView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="SQLite数据库"><button android:id="@+id/createDatabase" android:layout_below="@id/textView" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="创建一个数据库"></button><button android:id="@+id/createTable" android:layout_below="@id/createDatabase" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="创建一张表"></button><button android:id="@+id/insert" android:layout_below="@id/createTable" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="插入数据"></button><button android:id="@+id/select" android:layout_below="@id/insert" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="查询数据"></button><button android:id="@+id/update" android:layout_below="@id/select" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="更新数据"></button><button android:id="@+id/delete" android:layout_below="@id/update" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="删除数据"></button></textview></relativelayout>

 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

posted @ 2015-01-28 10:27  回忆安在  阅读(152)  评论(0编辑  收藏  举报