String
dbName = "test.db";
int
dbMode = Context.MODE_PRIVATE;
SQLiteDatabase
db = context.openOrCreateDatabase(dbName, dbMode, null);
String
sql = null;
try{
sql
= "DROP
TABLE table1;";
db.execSQL(sql);
Log.d("sqliteTest",
"drop
table");
}catch(SQLException
e){
Log.e("sqliteTest",
e.toString());
}
try{
sql
= "CREATE
TABLE table1 (id text primary key, name text);";
db.execSQL(sql);
Log.d("sqliteTest",
"create
table");
}catch(SQLException
e){
Log.e("sqliteTest",
e.toString());
}
try{
ContentValues
initialValues = new
ContentValues();
initialValues.put("id","1");
initialValues.put("name","one");
db.insert("table1",
null,
initialValues);
Log.d("sqliteTest",
"insert
values");
}catch(SQLException
e){
Log.e("sqliteTest",
e.toString());
}
try{
sql
= "select
* from table1;";
Cursor
result = db.rawQuery(sql, null);
if(result.moveToFirst()){
String
id = result.getString(0);
String
name = result.getString(result.getColumnIndex("name"));
Log.d(null,
"id
: "
+ id + ",
name : "+name);
}
}catch(SQLException
e){
Log.e("sqliteTest",
e.toString());
}
try{
db.close();
}catch(SQLException
e){
Log.e("sqliteTest",
e.toString());
}
댓글
댓글 쓰기