1.普通的sql語(yǔ)句
2.crud直接寫(xiě)sql語(yǔ)句
3.curd占位符
* onUpgrade方法中實(shí)現(xiàn)
//當(dāng)數(shù)據(jù)庫(kù)的版本號(hào)變大時(shí)調(diào)用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "onUpgrade---oldVersion:" + oldVersion + "## newVersion:" + newVersion);
//升級(jí)修改數(shù)據(jù)庫(kù)中內(nèi)容隘马,添加表
db.execSQL("create table info (id,name,phone)");
}
## 數(shù)據(jù)庫(kù)的curd-直接寫(xiě)sql語(yǔ)句 ##
* 增
/**
* 指定用戶名和金額插入一條記錄
*
* @param name
* @param money
*/
public void insert(String name, float money) {
// 1.創(chuàng)建helper對(duì)象
// BankDBOpenHelper helper = new BankDBOpenHelper(context);
// 2.獲得可讀寫(xiě)數(shù)據(jù)庫(kù)
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "insert into account (name,money) values ('" + name + "',"
+ money + ")";
Log.d(TAG, sql);
// 3.執(zhí)行插入sql語(yǔ)句
db.execSQL(sql);
}
* 刪
// 刪 delete from account where name='zhangsan'
/**
* 指定用戶名疮胖,刪除表中一行記錄
*
* @param name
*/
public void delete(String name) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("delete from account where name='" + name + "'");
}
* 改
// 改 update account set money=19.9 where name='zhangsan'
/**
* 指定用戶名修改用戶金額
*
* @param name
* @param money
*/
public void update(String name, float money) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("update account set money=" + money + " where name='" + name
+ "'");
}
* 查
// 查 select money from account where name='zhangsan'
/**
* 指定用戶查金額
* @param name
* @return -1表示未查到記錄;正數(shù)辆毡,查出結(jié)果
*/
public float query(String name) {
SQLiteDatabase db = helper.getReadableDatabase();
// 調(diào)用rawQuery查詢
Cursor cursor = db.rawQuery("select money from account where name='"
+ name + "'", null);
// cursor 游標(biāo),指針,提供移動(dòng)到第一條記錄碴倾,移動(dòng)到下一條記錄蜒什,移動(dòng)到最后一條記錄
// cursor.moveToFirst()
// cursor.moveToNext()
// cursor.moveToLast()
float money = -1;//-1表示未查到記錄
//判斷是否存在第一條數(shù)據(jù)
if(cursor.moveToFirst()){
//取出指定列的數(shù)據(jù)
//參數(shù)是以0為起點(diǎn)數(shù)組下標(biāo) money (0) name (1) id (2)
//money = cursor.getFloat(0);
money = cursor.getFloat(cursor.getColumnIndex("money"));
}
cursor.close();//cursor必須關(guān)閉嚎莉,否則會(huì)有內(nèi)存泄露(申請(qǐng)了沒(méi)釋放)关拒。
return money;
}
## 內(nèi)存有關(guān)錯(cuò)誤說(shuō)明 ##
* 內(nèi)存溢出:要申請(qǐng)的內(nèi)存比現(xiàn)有內(nèi)存大,內(nèi)存不夠
* 內(nèi)存泄露:申請(qǐng)內(nèi)存空間蝉揍,不再使用內(nèi)存時(shí)链峭,不釋放
## curd-占位符 ##
* 改
// db.execSQL("update account set money=" + money + " where name='" + name
// + "'");
db.execSQL("update account set money=? where name=?", new Object[]{money,name});
* 插入
// String sql = "insert into account (name,money) values ('" + name + "',"
// + money + ")";
// 3.執(zhí)行插入sql語(yǔ)句 ?占位符,表示此處應(yīng)有數(shù)據(jù)
db.execSQL("insert into account (name,money) values (?,?)",new Object[]{name,money});
* 刪
// db.execSQL("delete from account where name='" + name + "'");
db.execSQL("delete from account where name=?", new Object[]{name});
* 查
// 調(diào)用rawQuery查詢
// Cursor cursor = db.rawQuery("select money from account where name='"
// + name + "'", null);
Cursor cursor = db.rawQuery("select money from account where name=?", new String[]{name});
## curd-谷歌api ##
* 增
// 3.執(zhí)行插入sql語(yǔ)句 ?占位符又沾,表示此處應(yīng)有數(shù)據(jù)
//db.execSQL("insert into account (name,money) values (?,?)",new Object[]{name,money});
ContentValues values = new ContentValues();
values.put("name", name);
values.put("money", money);
/**
* 參數(shù)1 table 要插入表名
* 參數(shù)2 nullColumnHack 空列處理弊仪,一般null
* 參數(shù)3 values是一個(gè)map集合,key(列名)-value(列的值)
*/
long rowId = db.insert("account", null, values);
* 刪
// db.execSQL("delete from account where name=?", new Object[]{name});
//account? name=? new Object[]{name}
/**
* 參數(shù)1 表名
* 參數(shù)2 刪除條件,where后面部分杖刷,帶?占位符
* 參數(shù)3 刪除條件中?對(duì)應(yīng)數(shù)據(jù)
*/
int delete = db.delete("account", "name=?", new String[]{name});
* 改
//db.execSQL("update account set money=? where name=?", new Object[]{money,name});
ContentValues values = new ContentValues();
values.put("money", money);
// account? money=?(contentValues集合保存)? name=?
db.update("account", values , "name=?", new String[]{name});
* 查
//Cursor cursor = db.rawQuery("select money from account where name=?", new String[]{name});
//money,id? account where name=?
/**
* 參數(shù)1 表名
* 參數(shù)2 字符串?dāng)?shù)組保存要查出哪些列
* 參數(shù)3 where后面條件励饵,帶?
* 參數(shù)4 where后面條件中占位符?,對(duì)應(yīng)數(shù)據(jù)
* 參數(shù)5 null
* 參數(shù)6 null
* 參數(shù)7 null
*/
Cursor cursor = db.query("account", new String[]{"money","id"}, "name=?", new String[]{name}, null, null, null);