SQLite資料庫增刪改查

來源:互聯網
上載者:User

標籤:float   new   tor   content   1.0   ade   ble   bundle   open   

在安卓開發中不可避免的會遇到在手機中儲存資料的時候,如果只是小量資料(如儲存設定等)的話,用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     @Override16     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 <Button10     android:id="@+id/button1"11     android:layout_width="match_parent"12     android:layout_height="wrap_content"13     android:onClick="onClick"14     android:text="增"/>15     <Button16         android:id="@+id/button2"17         android:layout_width="match_parent"18         android:layout_height="wrap_content"19         android:onClick="onClick"20         android:text="刪"/>21     <Button22         android:id="@+id/button3"23         android:layout_width="match_parent"24         android:layout_height="wrap_content"25         android:onClick="onClick"26         android:text="改"/>27     <Button28         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資料庫增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.