1:創建一個類,繼承SQLiteOpenHelper這個抽象類;代碼如下
下面代碼是先新建一個表; 表明是Book
public class MySQLite extends SQLiteOpenHelper { PRivate Context context; //表 注意()前,表明和括號是有空格的,用來區分表明; public static final String CREATE_BOOK = "create table Book (id integer not null primary key autoincrement,name varchar not null,author varchar not null,price real,amount integer not null)"; public MySqLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); this.context = context; //這里記得初始化 } @Override public void onCreate(SQLiteDatabase db) { //創建sql表; db.execSQL(CREATE_BOOK); Toast.makeText(context, "數據庫表創建成功", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}2: 在學到的地方創建數據庫; 例如;點擊后創建 onclick()中;
//創建數據庫 SQLite MySqLite mySqLite = new MySqLite(this, "BookList.db", null, 1); 號++ //通過getReadableDatabase()創建或打開數據庫; mySqLite.getReadableDatabase();3:修改數據庫,添加另一個表; 需使用更新的方法; 首先先要在MySqlite中寫創建表的語句;
//更新數據庫 //更新時新加的 public static final String CREATE_TYPE = "create table Type (id integer not null primary key autoincrement,type varchar not null)";在oncreate里:
@Override public void onCreate(SQLiteDatabase db) { //創建sql表; db.execSQL(CREATE_BOOK); db.execSQL(CREATE_TYPE);//更新時新加的 Toast.makeText(context, "數據庫表創建成功", Toast.LENGTH_SHORT).show(); }3: 更新方法里:
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //更新時新加的 //利用drop來刪除,這樣更新內容; db.execSQL("drop table if exists Book"); db.execSQL("drop table if exists Type"); onCreate(db); }4: 最后還需要把之前的創建的地方修改一下版本號;
MySqLite mySqLite = new MySqLite(this, "BookList.db", null, 2); //更新后更改版本號++這時這個BookList.db文件中就存在兩個表;
5: 增刪改查 下面是使用方法 具體實現還需要自己處理,這里都寫在一起了;
//通過getReadableDatabase()創建或打開數據庫; SQLiteDatabase database = mySqLite.getReadableDatabase();// sqLiteDatabase.beginTransaction(); //添加數據 ContentValues values = new ContentValues(); values.put("name", "水傳"); values.put("author", "小水"); values.put("price", 50); values.put("amount", 1000); long insert = database.insert("Book", null, values); if(insert>0) { Toast.makeText(DataSaveActivity.this, "水傳添加成功", Toast.LENGTH_SHORT).show(); } values.clear(); // values.put("name", "梨花"); values.put("author", "小明"); values.put("price", 60); values.put("amount", 500); long insert1 = database.insert("Book", null, values); if(insert1>0) { Toast.makeText(DataSaveActivity.this, "梨花添加成功", Toast.LENGTH_SHORT).show(); } values.clear(); // //修改; values.put("author", "derM"); int update = database.update("Book", values, "name = ?", new String[]{"水傳"}); if(update>0) { Toast.makeText(DataSaveActivity.this, "水傳作者已修改", Toast.LENGTH_SHORT).show(); } values.clear(); //刪除: int delete = database.delete("Book", "name = ?", new String[]{"梨花"}); if(delete>0) { Toast.makeText(DataSaveActivity.this, "刪除梨花成功", Toast.LENGTH_SHORT).show(); }//查 Cursor cursor = database.query("Book", null, null, null, null, null, null); if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); int price = cursor.getInt(cursor.getColumnIndex("price")); int amount = cursor.getInt(cursor.getColumnIndex("amount")); LogUtil.e(TAG,"name: "+name+", author: "+author+", price: "+price+", amount: "+amount); } while (cursor.moveToNext()); } cursor.close();//使用完記得關閉;這里還有數據庫的事物沒有說明; 事務的作用:保證數據的準確性等;如果在結束時沒有執行到成功標記,則之間的所有數據庫操作都會回滾,變為和之前就是一樣的;如果執行到了 則數據改變;
sqLiteDatabase.beginTransaction();//這之間執行數據庫操作語句;、、、、、 sqLiteDatabase.setTransactionSuccessful(); sqLiteDatabase.endTransaction();新聞熱點
疑難解答