AndroidDbHelperの作成



Android Dbhelper Creation



この記事はあなた自身の記録なので、後で確認することができます。

public class DbHelper extends SQLiteOpenHelper { private SQLiteDatabase db = null private Cursor cursor = null private String TAG = 'DBTest' private boolean isInit private String createString public DbHelper(Context context, String dbName, SQLiteDatabase.CursorFactory factory, int version) { super(context, dbName, factory, version) } // build the table @Override public void onCreate(SQLiteDatabase db) { this.db = db db.execSQL(createString) } public void inItDb(String createString) { if (isInit) return this.createString = createString this.isInit = true } // increase public void insert(ContentValues values, String tableName) { SQLiteDatabase db = getWritableDatabase() System.out.println('values' + values.get('id') + values.get('name') + values.get('age')) db.insert(tableName, null, values) Log.i (TAG, 'Add a line') db.close() } // delete a row public void delete(String id, String tableName) { // if (db == null) { SQLiteDatabase db = getWritableDatabase() // } db.delete(tableName, 'id=?', new String[]{String.valueOf(id)}) Log.i (TAG, 'delete one line') } // update a row public void update(ContentValues values, String string, String tableName) { SQLiteDatabase db = getWritableDatabase() db.update(tableName, values, 'id=?', new String[]{string}) db.close() Log.i (TAG, 'Update a line') } // Query by id public Cursor query(String string, String tableName) { SQLiteDatabase db = getWritableDatabase() System.out.println('id---->' + string) cursor = db.query(tableName, null, 'id=?', new String[]{string}, null, null, null) Log.i (TAG, 'Query a row by id') return cursor } public Cursor query(String tableName) { SQLiteDatabase db = getWritableDatabase() cursor = db.query(tableName, null, null, null, 'id', null, 'id') Log.i (TAG, 'Query all') return cursor } public boolean deleteDatabase(Context context, String DBName) { return context.deleteDatabase(DBName) } / / Press the sql statement to operate the database public void handleBySql(String TabName) { String sqsl = 'update sqlite_sequence set seq=0 where name='' + TabName + ''' SQLiteDatabase db = getWritableDatabase() db.execSQL(sqsl) Log.i (TAG, 'Execute sql statement') } /** * Determine if a table exists * * @return */ public boolean tabbleIsExist(String tableName) { boolean result = false if (tableName == null) { return false } SQLiteDatabase db = null Cursor cursor = null try { db = this.getReadableDatabase() String sql = 'select count(*) as c from Sqlite_master where type ='table' and name ='' + tableName.trim() + '' ' cursor = db.rawQuery(sql, null) if (cursor.moveToNext()) { int count = cursor.getInt(0) if (count > 0) { result = true } } } catch (Exception e) { // TODO: handle exception } return result } / / Close the database public void close() { if (db != null) { db.close() db = null } if (cursor != null) { cursor.close() cursor = null } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } / / Clear the search history public void deleteAllRecords() { db = this.getWritableDatabase() db.execSQL('delete from TabOther') db.close() } /** * Create a table * * @return */ public static String getOther(String tablename) { return 'Create Table ' + tablename + '( id text not null, name text )' } / / Use in the Activity / / Initialize dbHelper = new DbHelper(this, DB, null, 1) dbHelper.inItDb(dbHelper.getOther(TabOther)) //adding data ContentValues values = new ContentValues() values.put('id', num + '') values.put('name', name + '') dbHelper.insert(values, TabOther) //Query data /** * Query database * * @return */ public List queryAll() { List rst = new ArrayList() // parse the cursor Cursor cursor = dbHelper.query(TabOther) CoordinateAlterSample persion = null if (cursor.moveToFirst()) { while (!cursor.isAfterLast()) { persion = new CoordinateAlterSample() persion.id = cursor.getString(cursor.getColumnIndex('id')) persion.name = cursor.getString(cursor.getColumnIndex('name')) rst.add(persion) cursor.moveToNext() } } return rst } //modify