Android数据储存之SQLiteDatabase 简单增删改查

SQLiteDatabase 使用

SQLiteDatabase提供如下方法来打开一个文件对应的数据库:

openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)  打开path文件所代表的文件

openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)   打开或创建(如果不存在时)path文件所代表的文件

openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory) 打开或创建File(如果不存在时)文件所代表的文件

以上方法均返回一个SQLiteDatabase对象!

调用SQLiteDatabase对象中的方法来执行sql操作数据库:

execSQL(String sql) 执行sql


 

execSQL(String sql, Object[] bindArgs)  执行sql

bindArgs代表sql中的占位符参数的值


 

insert(String table, String nullColumnHack, ContentValues values) 插入语句,

table表名

valuesContentValues对象!该对象存储方式为Key-value,Key表示表中的字段名称,value表示该字段对应的值

nullColumnHack表示强行向表中插入null,前提是第三个参数(values)为空或者不包含任何Key-value时生效


 

update(String table, ContentValues values, String whereClause, String[] whereArgs)  更新语句

table为表名

valuesContentValues对象!该对象存储方式为Key-value,Key表示表中的字段名称

value表示该字段对应的值,第三个参数为条件例如:_ID = ? ,第四个参数为第三个参数的值!


 

delete(String table, String whereClause, String[] whereArgs)  删除数据

table表名

whereClause为条件例如:_ID = ?

whereArgswhereClause的值!


 

query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)  查询语句

table为表名

columns想要显示的字段

selection条件

selectionArgs 中为selection的值

groupBy 分组

having  分组条件

orderBy 排序

limit 分页


 

 1 public class MainActivity extends Activity implements OnClickListener{
 2     private Button button,btnUpdate,btnDelete,btnQuery;
 3     private static String DATABASE_PATH;
 4     private static String DATABASE_SQL;
 5     private SQLiteDatabase database;
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         initView();
11         setEvent();
12     }
13 
14     private void setEvent() {
15         button.setOnClickListener(this);
16         btnUpdate.setOnClickListener(this);
17         btnDelete.setOnClickListener(this);
18         btnQuery.setOnClickListener(this);
19     }
20 
21     private void initView() {
22         DATABASE_PATH = this.getFilesDir()+"/myDataBase.db";
23         DATABASE_SQL = "CREATE TABLE USER_INOF(_ID INTEGER PRIMARY KEY,_NAME,_AGE,_ADDRESS)";
24         database = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH, null);
25         button = (Button) findViewById(R.id.id_databaseButton);
26         btnUpdate = (Button) findViewById(R.id.id_databaseButtonUpdate);
27         btnDelete =(Button) findViewById(R.id.id_databaseButtonDelete);
28         btnQuery= (Button) findViewById(R.id.id_databaseButtonQuery);
29     }
30 
31     @Override
32     public void onClick(View v) {
33         switch (v.getId()) {
34         case R.id.id_databaseButton:
35             //database.execSQL(DATABASE_SQL);
36             ContentValues values = new ContentValues();
37             values.put("_NAME", "悟空");
38             values.put("_AGE", "666");
39             values.put("_ADDRESS", "辽宁");
40             long i = database.insert("USER_INOF", null, values);
41             if(i==-1){
42                 Toast.makeText(this, "添加失败", 0).show();
43             }else{
44                 Toast.makeText(this, "添加成功", 0).show();
45             }
46             break;
47         case R.id.id_databaseButtonUpdate:
48             ContentValues value = new ContentValues();
49             value.put("_NAME", "猪八戒");
50             value.put("_AGE", "555");
51             value.put("_ADDRESS", "辽宁");
52             database.update("USER_INOF", value, "_ID=?",new String[]{"2"});
53             break;
54         case R.id.id_databaseButtonDelete:
55             int o = database.delete("USER_INOF", "_ID > ?", new String[]{"1"});
56             Toast.makeText(this,""+o+"条数据受影响", 0).show();
57             break;
58         case R.id.id_databaseButtonQuery:
59             Cursor cursor =database.query("USER_INOF", null, null, null, null, null, null);
60             if(cursor.getCount()>0){
61                 while (cursor.moveToNext()) {
62                     String id = cursor.getString(0);
63                     String name =cursor.getString(1);
64                     String age =cursor.getString(2);
65                     String address =cursor.getString(3);
66                     Toast.makeText(this, "查询成功:"+id+name+age+address, 0).show();
67                 }
68             }else{
69                 Toast.makeText(this,""+cursor.getCount()+"", 0).show();
70             }
71             break;
72         default:
73             break;
74         }
75     }
76 
77 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:orientation="vertical" >
 5 
 6     <Button
 7         android:id="@+id/id_databaseButton"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:text="添加数据" />
11 
12     <Button
13         android:id="@+id/id_databaseButtonUpdate"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="修改数据" />
17 
18     <Button
19         android:id="@+id/id_databaseButtonDelete"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="删除数据" />
23 
24     <Button
25         android:id="@+id/id_databaseButtonQuery"
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="查询数据" />
29 
30 </LinearLayout>

posted @ 2016-01-28 02:23  一天学点  阅读(7757)  评论(0编辑  收藏  举报