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)
//創(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);
//查詢 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);
}
執(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);
}
您可能感興趣的文章:
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- Android使用SQLite數(shù)據(jù)庫的簡單實例
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
- 深入Android SQLite 事務(wù)處理詳解
- Android--SQLite(增,刪,改,查)操作實例代碼
- android中sqlite的按條件查找的小例子
- android開發(fā)教程之listview顯示sqlite數(shù)據(jù)
- Android SQLite數(shù)據(jù)庫增刪改查操作的案例分析
- Android中操作SQLite數(shù)據(jù)庫快速入門教程
- Android提高之SQLite分頁表格實現(xiàn)方法
- Android使用Sqlite存儲數(shù)據(jù)用法示例
相關(guān)文章
Android 實現(xiàn)會旋轉(zhuǎn)的餅狀統(tǒng)計圖實例代碼
這篇文章主要介紹了Android 實現(xiàn)會旋轉(zhuǎn)的餅狀統(tǒng)計圖實例代碼的相關(guān)資料,這里附有實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下2016-12-12Android自定義帶水滴的進(jìn)度條樣式(帶漸變色效果)
這篇文章主要介紹了Android自定義帶水滴的進(jìn)度條樣式(帶漸變色效果)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12詳解Android TextView屬性ellipsize多行失效的解決思路
這篇文章主要介紹了Android TextView屬性ellipsize多行失效的解決思路,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07android中實現(xiàn)OkHttp下載文件并帶進(jìn)度條
本篇文章主要介紹了android中實現(xiàn)OkHttp下載文件并帶進(jìn)度條,OkHttp是比較火的網(wǎng)絡(luò)框架,它支持同步與異步請求,支持緩存,可以攔截,更方便下載大文件與上傳文件的操作,有興趣的可以了解一下2017-07-07Android仿餓了么加入購物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動畫的按鈕效果(實例詳解)
這篇文章主要介紹了Android仿餓了么加入購物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動畫的按鈕效果(實例詳解)的相關(guān)資料,需要的朋友可以參考下2017-01-01