SQLite数据库增删改查

在安卓开发中不可避免的会遇到在手机中保存数据的时候,如果只是小量数据(如保存设置等)的话,用SharedPreferences是个极好的选择,它以键值对的形式保存数据,但是如果数据量比较多的话,比如一个键对应了一个集合的情况,此时再用SharedPreferences保存数据就显得吃力了,如果再需要对数据进行修改删除的操作,这个保存数据的方法明显不适合了,所以安卓本身也内置了sqlite数据库,对于保存app里面的数据已经够了。

新建安卓工程后新建一个类:SQLDatabase .java,该类继承自SQLiteOpenHelper,主要用于新建数据库,新建数据表和更新数据库:

 1 public class SQLDatabase extends SQLiteOpenHelper{
 2     private Context context;
 3     private String CREAT_BOOK="create table Book(id integer primary key autoincrement,author text,price real,pages integer,name text)";
 4     public SQLDatabase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
 5         super(context, name, factory, version);
 6         this.context = context;
 7     }
 8     @Override
 9     public void onCreate(SQLiteDatabase db) {
10         //建表
11         db.execSQL(CREAT_BOOK);
12         //插入一些数据
13         Toast.makeText(context, "创建数据库成功", Toast.LENGTH_SHORT).show();
14     }
15     @Override
16     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
17         Toast.makeText(context, "数据库更新了", Toast.LENGTH_SHORT).show();
18     }
19 }

布局文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="com.example.mydiyizho3_2.MainActivity">
 9 <Button
10     android:id="@+id/button1"
11     android:layout_width="match_parent"
12     android:layout_height="wrap_content"
13     android:onClick="onClick"
14     android:text="增"/>
15     <Button
16         android:id="@+id/button2"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:onClick="onClick"
20         android:text="删"/>
21     <Button
22         android:id="@+id/button3"
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content"
25         android:onClick="onClick"
26         android:text="改"/>
27     <Button
28         android:id="@+id/button4"
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content"
31         android:onClick="onClick"
32         android:text="查"/>
33 
34 </LinearLayout>

接下来就是增加,删除,修改,查找数据:

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     private static final String TAG = "MainActivity";
 4     private SQLDatabase database;
 5     private SQLiteDatabase writableDatabase;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         database = new SQLDatabase(MainActivity.this, "BookStore.db", null, 1);
12     }
13 
14     public void onClick(View view) {
15         switch (view.getId()){
16             case R.id.button1://
17                 writableDatabase = database.getWritableDatabase();
18                 ContentValues values = new ContentValues();
19                 for (int i = 0; i <20 ; i++) {
20                     values.put("author","homeng"+i);
21                     values.put("price",10.5+i);
22                     values.put("pages",1000+i);
23                     values.put("name","杂技团"+i);
24                     long book = writableDatabase.insert("Book", null, values);
25                     Log.i(TAG,"每天的id="+book);
26                 }
27                 //关闭
28                 writableDatabase.close();
29                 break;
30             case R.id.button2://
31                 writableDatabase = database.getWritableDatabase();
32                 int book = writableDatabase.delete("Book", "id=?", new String[]{1 + ""});
33                 Log.i(TAG,"这是删除的"+book);
34                 writableDatabase.close();
35                 break;
36             case R.id.button3://
37                 writableDatabase = database.getWritableDatabase();
38                 ContentValues update = new ContentValues();
39                 update.put("price",10000);
40                 int update1 = writableDatabase.update("Book", update, "author=?", new String[]{"homeng5"});
41                 Log.i(TAG,"这是修改的"+update1);
42                 writableDatabase.close();
43                 break;
44             case R.id.button4://
45                 writableDatabase = database.getWritableDatabase();
46                 Cursor book1 = writableDatabase.query("Book", null, null, null, null, null, null);
47                 if (book1.moveToFirst()){
48                     do {
49                         String name = book1.getString(book1.getColumnIndex("name"));
50                         float price = book1.getFloat(book1.getColumnIndex("price"));
51                         int pages = book1.getInt(book1.getColumnIndex("pages"));
52                         String author = book1.getString(book1.getColumnIndex("author"));
53                         Log.i(TAG,"name"+name);
54                         Log.i(TAG,"price"+price);
55                         Log.i(TAG,"pages"+pages);
56                         Log.i(TAG,"author"+author);
57                     }while (book1.moveToNext());
58                 }
59                 writableDatabase.close();
60                 break;
61         }
62     }
63 }
原文地址:https://www.cnblogs.com/SongYongQian/p/7850160.html