android中数据库sqlite的增删改查

【增删改查】
package com.test.helloworld;


import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class AccountDao {
private MyHelper myHelper;
public AccountDao(Context context) {
myHelper = new MyHelper(context);
}
public void delete(Integer id){
SQLiteDatabase db = myHelper.getWritableDatabase();
db.execSQL("delete from account where id=?",new Object[] {id});
db.close();
}
public void insert(Account a){
SQLiteDatabase db = myHelper.getWritableDatabase();
/*db.execSQL("insert into account(name,balance) values ("+a.getName()+","+a.getBalance()+")");*/
db.execSQL("insert into account(name,balance) values (?,?)",new Object[]{a.getName(),a.getBalance()});
db.close();
}
public Account query(Integer id){
SQLiteDatabase db = myHelper.getReadableDatabase();
Cursor c = db.rawQuery("select name,balance from account where id=?"
,new String[]{id+""});
Account a = null;
while(c.moveToNext()){
String name=c.getString(0);
int balance=c.getInt(1);
a = new Account(id,name,balance); 
}
c.close();
db.close();
return a;
}
public void update(Account a){
SQLiteDatabase db = myHelper.getWritableDatabase();
db.execSQL("update account set name=?,balance=? where id=?"
,new Object[]{a.getName(),a.getBalance(),a.getId()});
db.close();
}
public List<Account> queryAll(){
SQLiteDatabase db = myHelper.getReadableDatabase();
Cursor c = db.rawQuery("select id,name,balance from account",null);
List<Account> list=new ArrayList<Account>();
Account a=null;
while(c.moveToNext()){
Integer id=c.getInt(0);
String name=c.getString(1);
Integer balance=c.getInt(2);
a = new Account(id,name,balance);
list.add(a);
}
c.close();
db.close();
return list;

}
public List<Account> queryPage(int pageNum,int pageSize){
String start = (pageNum-1)*pageSize+"";
String size=pageSize+"";
SQLiteDatabase db = myHelper.getReadableDatabase();
Cursor c = db.rawQuery("select id,name,balance from account limit ?,?",new String[]{start,size});
List<Account> list=new ArrayList<Account>();
Account a=null;
while(c.moveToNext()){
Integer id=c.getInt(c.getColumnIndex("id"));
String name=c.getString(c.getColumnIndex("name"));
Integer balance=c.getInt(c.getColumnIndex("balance"));
a = new Account(id,name,balance);
list.add(a);
}
c.close();
db.close();
return list;
}
public int queryCount(){
SQLiteDatabase db = myHelper.getReadableDatabase();
Cursor c = db.rawQuery("select count(*) from account",null);
c.moveToNext();
int count = c.getInt(0);
c.close();
db.close();
return count;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值