【增删改查】
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;
}
}
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;
}
}