欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android操作SQLite增刪改減實現(xiàn)代碼

 更新時間:2010年11月09日 10:28:18   作者:  
android操作SQLite增刪改減實現(xiàn)代碼,學(xué)習(xí)android的朋友可以參考下。
如果一個應(yīng)用程序中的數(shù)據(jù)庫無需提供對外訪問,實現(xiàn)一個繼承自SQLiteOpenHelper的數(shù)據(jù)庫幫助類,以支持?jǐn)?shù)據(jù)庫的創(chuàng)建和版本的更新, 這些SQLiteDataBase所不能實現(xiàn)的.但是SQLiteDataBase卻具備一些非常重要的對數(shù)據(jù)庫進(jìn)行操作的方法,數(shù)據(jù)表的創(chuàng)建刪除、數(shù)據(jù) 的增刪改查都是通過它實現(xiàn)的。

執(zhí)行增刪改操作方法 :db.execSQL(sql); 或者db.insert()、db.delete()、db.update(),并且包括數(shù)據(jù)表的創(chuàng)建和刪除等等也可以通過execSQL實現(xiàn)
復(fù)制代碼 代碼如下:

//創(chuàng)建表
public boolean createTable(){
SQLiteDatabase db=dbHelper.getWritableDatabase();
String sql="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"(ID INTEGER PRIMARY KEY,Name VARCHAR,Age INTEGER)";
try{
db.execSQL(sql);
return true;
}catch(SQLException ex){
Log.d(tag, "create table failure");
return false;
}
}
//添加數(shù)據(jù)
public boolean addData(){
String name=etname.getText().toString();
String age=etage.getText().toString();
SQLiteDatabase db=dbHelper.getWritableDatabase();
String sql="insert into "+TABLE_NAME+"(name,age) values ('"+name+"','"+age+"')";
try{
db.execSQL(sql);
return true;
}catch(SQLException ex){
Log.d(tag, "add data failure");
return false;
}
}
//修改
public boolean updateData(){
SQLiteDatabase db=dbHelper.getWritableDatabase();
String sql="update "+TABLE_NAME+" set age='2' where name like 'cb'";
Object[] bindArgs={"cb"};
try{
db.execSQL(sql, bindArgs);
return true;
}catch(SQLException ex){
Log.d(tag, "update data failure");
return false;
}
}

執(zhí)行數(shù)據(jù)查詢方法:db.rawQuery(sql, selectionArgs); 或者db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
復(fù)制代碼 代碼如下:

//查詢 public void selectData(){
SQLiteDatabase db=dbHelper.getReadableDatabase();
String[] columns={"name"};
Cursor cursor=db.query(TABLE_NAME, columns, null, null, null, null, null);
String names="";
while(cursor.moveToNext()){
int c=cursor.getColumnIndexOrThrow("Name");
String name=cursor.getString(c);
//< = >
//String name=cursor.getString(0);//只查詢了一列
if(names==""){
names=name;
}else{
names=names+"\n"+name;
}
}
tvname.setText(names);
//另外一種查詢方法
//String sql="select name from "+TABLE_NAME;
//Curosr cursor=db.rawQuery(sql, null);
}

相關(guān)文章

最新評論