Android

using sqlite

aucd29 2013. 10. 8. 14:54
http://stackoverflow.com/questions/3684678/best-practices-of-working-with-multiple-sqlite-db-tables-in-android

- 주어야할 Uses-Permission
[code]
<uses-permission android:name="android.permission.READ_OWNER_DATA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_OWNER_DATA"></uses-permission>
[/code]

- CREATE DB and TABLE
[code]
class DBOpenHelper extends SQLiteOpenHelper {
    DBOpenHelper(Context context) {
        super(context, "testdb", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(getCreateQuery());
        // CREATE 문
        // EX)
        // CREATE TABLE testdb (no INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, tel TEXT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("", "@@@@@@@@@@@@@@@@@@@@@@ db onupgrade @@@@@@@@@@@@@@@@@@@@@@");
        db.execSQL("DROP TABLE IF EXISTS " + tableName);
        onCreate(db);
    }
}
[/code]

- INSERT
[code]
DBOpenHelper helper = new DBOpenHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("INSERT INTO testdb(name, tel) VALUES('aucd29', '010-1234-1234');");
// or
ContentValues val = new ContentValues();
val.put("name", "maru");
val.put("tel", "010-1234-1234");
db.insert("testdb", null, val);
[/code]

- SELECT
[code]
Cursor cr = db.rawQuery("SELECT * FROM testdb");
while(cr.moveToNext()) {
    Log.d("", "name : " + cr.getString(1));
    Log.d("", "tel : " + cr.getString(2)); // field position
}

// select 의 경우 android api 는 별로 매리트가 없어서 사용 포기 -_ -
Cursor cursor = db.query(TABLE_NAME,
new String[] {"_id", "title", "title_raw"},
"title_raw like " + "'%Smith%'", null, null, null, null);

Cursor cursor = db.query(TABLE_NAME,
new String[] {"_id", "title", "title_raw"},
"title_raw like =?, new String[]{"'%Smith%'"}, null, null, null);
[/code]

- UPDATE
[code]
db.execSQL("UPDATE testdb SET name='hello' WHERE no=1");
or
ContentValues values = new ContentsValues();
values.put("name", "hello");
db.update("testdb", values, "no=1", null);
[/code]

- DELETE
[code]
db.execSQL("DELETE FROM testdb WHERE no=1");
or
db.delete("testdb", "no=1", null);
[/code]

- TRANSACTION
[code]
try {
    db.beginTransation();
    db.execSQL("INSERT INTO testdb(name, tel) VALUES ('aucd29', '010-2345-1234');");
    db.setTransactionSuccessful();
} catch (Exception e) {
    e.printStatckTrace();
} finally {
    db.endTransaction();
}
[/code]

- ADAPTOR
[code]
// Perform a managed query. The Activity will handle closing and requerying the cursor
// when needed.
Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null,
                                NoteColumns.DEFAULT_SORT_ORDER);

// Used to map notes entries from the database to views
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                             R.layout.noteslist_item,
                             cursor,
                             new String[] { NoteColumns.TITLE },
                             new int[] { android.R.id.text1 });
setListAdapter(adapter);
[/code]