在android中我們通常會(huì)用到SQLiteOpenHelper來建立數(shù)據(jù)庫,因?yàn)樗麜鴮懞唵?,并且可以快速的升?jí)數(shù)據(jù)庫的版本。 下面我們對(duì)SQLiteOpenHelper進(jìn)行一一解答:
繼承SQLiteOpenHelper
public class MySQLiteOpenHelper extends SQLiteOpenHelper { //這里的bank是數(shù)據(jù)庫的表名;而"banke.db"是整個(gè)數(shù)據(jù)庫的文件名; PRivate static final String sql = "create table bank(id integer primary key autoincrement ,name varchar , money integer)"; private static final String TAG = MySQLiteOpenHelper.class.getSimpleName(); public MySQLiteOpenHelper(Context context) { //第二個(gè)參數(shù)是數(shù)據(jù)庫文件名; //第三個(gè)參數(shù)為 factory 游標(biāo)工廠,null 表示使用默認(rèn)的工廠; //version 為版本號(hào); super(context, "banke.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { Log.d(TAG, "onCreate: enter"); db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}sql語句:
增:String sql = "insert into bank (name , money ) values(?,?)"; database.execSQL(sql, new Object[]{"apple", 1800});刪:String sql = "delete from bank where name = ?"; database.execSQL(sql, new Object[]{"apple"});改:String sql = "update bank set money = ? where name =?"; database.execSQL(sql,new Object[]{1000,"apple"});查:String sql = "select (money) from bank where name = ?"; Cursor cursor1 = database.rawQuery(sql, new String[]{"apple"});使用方法有兩種:每次使用時(shí)都要得到
MySQLiteOpenHelper mySql = new MySQLiteOpenHelper(this);SQLiteDatabase mDatabase = mySql.getWritableDatabase();//第一種方法:/* insert(String table, String nullColumnHack, ContentValues values); String table 表示是數(shù)據(jù)表的表名(數(shù)據(jù)庫的文件名不等于數(shù)據(jù)表的表名這點(diǎn)得弄清楚) String nullColumnHack 當(dāng)下面的values為null時(shí)有效這個(gè)參數(shù)才有效,表示強(qiáng)行插入null值 ContentValues values */ ContentValues values = new ContentValues(); values.put("name", "apple"); values.put("money", 4999); mDatabase.insert("bank", null, values);//第二種方法://sql語句中的bank時(shí)數(shù)據(jù)庫的表名,values(?,?)里面的?號(hào)為占位符,這里的結(jié)果由execSQL中的new Object[]{}傳入; String sql = "insert into bank (name , money ) values(?,?)"; mDatabase.execSQL(sql, new Object[]{"apple", 1800});mDatabase.close();同樣也是兩種方法;
MySQLiteOpenHelper mySql = new MySQLiteOpenHelper(this);SQLiteDatabase mDatabase = mySql.getWritableDatabase();//第一種:/* String table 數(shù)據(jù)表的表名 String[] columns, 要查詢出來的列名; String selection, 相當(dāng)于select語句where 后面的部分,可提供占位符, String[] selectionArgs,為上一個(gè)占位符提供參數(shù), String groupBy, String having, String orderBy */Cursor cursor = mDatabase.query("bank", new String[]{"money"}, "name = ?", new String[]{"HUAWEI"}, null, null, null);//第二種://sql中 select 表示要查的內(nèi)容 from 數(shù)據(jù)庫表 where name = ? "?"號(hào)代表占位符; 下面這句話表示查詢name為 ?的一整行數(shù)據(jù);String sql = "select (money) from bank where name = ?"; Cursor cursor1 = mDatabase.rawQuery(sql, new String[]{"apple"}); while (cursor1.moveToNext()) { //int apple = cursor.getInt(cursor.getColumnIndex("money")); int huawei = cursor1.getInt(cursor1.getColumnIndex("money")); Log.d(TAG, "query: huawei = " + huawei); } cursor.close();mDatabase.close();//如果是要查詢整張表那么select語句后面的where部分就不需要了,如下;//查詢整張表的所有內(nèi)容;String sql = "select * from bank";Cursor cursor = database.rawQuery(sql, null);新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注