1. 程式人生 > >SQLite數據庫增刪改查

SQLite數據庫增刪改查

lda writable switch books ide red ear create val

在安卓開發中不可避免的會遇到在手機中保存數據的時候,如果只是小量數據(如保存設置等)的話,用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 }

SQLite數據庫增刪改查