1.设计好数据库的表结构,用android提供的SQLiteOpenHelper接口来实现数据库的创建
。。。。
public class DBOpenHelper extends SQLiteOpenHelper{
private static final int VERSION = 1;
private static final String DBNAME ="account.db";
public DBOpenHelper(Context context){ //定义构造函数
super(context,DBNAME,null,VERSION);
//重写基类的构造函数
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO 自动生成的方法存根
db.execSQL("create table tb_outacount(_id integer primary key,money decimal,time varchar(10),"+
"type varchar(10),address varchar(100),mark varchar(2000))");
db.execSQL("create table tb_inacount(_id integer primary key,money decimal,time varchar(10),"+
"type varchar(10),handler varchar(100),mark varchar(200))");
db.execSQL("create table tb_pwd(password varchar(20))");
db.execSQL("create table tb_flag(_id integer primary key,flag varchar(200))");
}
。。。。。。
2.根据tb_inacount 表结构(收入信息录入表),创建这个数据库表操作(增,删,改,查)公共类,写在****.***.DAO这个包下面
public class InacountDAO {
private DBOpenHelper helper;
private SQLiteDatabase db;
public InacountDAO(Context context){
helper = new DBOpenHelper(context);
}
/**
* 添加收入记录
*/
public void add(Tb_inacount tb_inacount){
db = helper.getWritableDatabase();
db.execSQL("insert into tb_inacount(_id,money,time,type,handler,mark) values (?,?,?,?,?,?)",
new Object[]{tb_inacount.get_id(),tb_inacount.getMoney(),tb_inacount.getTime(),tb_inacount.getType(),
tb_inacount.getHandle(),tb_inacount.getMark()});
}
/**
* 修改收入记录
* @param tb_inacount
*/
public void update(Tb_inacount tb_inacount){
db = helper.getWritableDatabase();
db.execSQL("update tb_inacount set money=?,time =?,type=?,handler=?,mark=? where _id=?",
new Object[]{tb_inacount.getMoney(),tb_inacount.getTime(),tb_inacount.getType(),
tb_inacount.getHandle(),tb_inacount.getMark(),tb_inacount.get_id()});
}
/**
* 返回结果集
* @param id
* @return
*/
public Tb_inacount find(int id){
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from tb_inacount where _id=?", new String[]{String.valueOf(id)});
if(cursor.moveToNext()){
return new Tb_inacount(cursor.getInt(cursor.getColumnIndex("_id")),cursor.getDouble(cursor.getColumnIndex("money")),
cursor.getString(cursor.getColumnIndex("time")),cursor.getString(cursor.getColumnIndex("type")),
cursor.getString(cursor.getColumnIndex("handler")),cursor.getString(cursor.getColumnIndex("mark")));
}else{
return null;
}
}
/**
* 删除一系列不选定的id
* @param ids
*/
public void delete(Integer... ids){
if(ids.length > 0){
StringBuffer sb = new StringBuffer();
for(int i=0;i
sb.append('?').append(',');
}
sb.deleteCharAt(sb.length() - 1);
db = helper.getWritableDatabase();
db.execSQL("delete from tb_inacount where _id in("+sb+")",(Object[])ids);
}
}
/**
* 返回Cursor 对象 供ListView SimpleCursorAdapter使用
* @return
*/
public Cursor getInAllDataCursor(){
db = helper.getWritableDatabase();
return db.rawQuery("select * from tb_inacount", null);
}
/**
* 分页显示收入结果集
* @param start 开始的位置
* @param count 一页显示几条
* @return
*/
public List getScrolData(int start,int count){
List tb_inacount = new ArrayList();
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from tb_inacount limit ?,?",
new String[]{String.valueOf(start),String.valueOf(count)});
while(cursor.moveToNext()){
tb_inacount.add(new Tb_inacount(cursor.getInt(cursor.getColumnIndex("_id")),cursor.getDouble(cursor.getColumnIndex("money")),
cursor.getString(cursor.getColumnIndex("time")),cursor.getString(cursor.getColumnIndex("type")),
cursor.getString(cursor.getColumnIndex("handler")),cursor.getString(cursor.getColumnIndex("mark"))));
}
return tb_inacount;
}
/**
* 返回当前结果集条数
* @return
*/
public long getCount(){
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select count(_id) from tb_inacount",null);
if(cursor.moveToNext()){
return cursor.getLong(0);
}else{
return 0;
}
}
/**
* 获取当前数据表中最大的ID
* @return
*/
public int getMaxId(){
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select max(_id) from tb_inacount", null);
if(cursor.moveToNext()){
return cursor.getInt(0);
}else{
return 0;
}
}
public Double getInSum(){
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select sum(money) from tb_inacount", null);
if(cursor.moveToNext()){
return cursor.getDouble(0);
}else{
return 0.0;
}
}
}
3. activity获取数据后,调用该方法对数据库进行操作
//数据库操申明对象
private Tb_inacount tb_inacount;
private InacountDAO inacountDAO;
// 为数据库操作申明对象
tb_inacount = new Tb_inacount(); //初始化
inacountDAO = new InacountDAO(AddinacountActivity.this);
获取数据并进行数据库操作
。。。。。
tb_inacount.set_id(inacountDAO.getMaxId()+1);
tb_inacount.setMoney(Double.parseDouble(money.getText().toString()));
tb_inacount.setTime(time.getText().toString());
tb_inacount.setType(type.getSelectedItem().toString());
tb_inacount.setHandle(handle.getText().toString());
tb_inacount.setMark(beizhu.getText().toString());
inacountDAO.add(tb_inacount);
new AlertDialog.Builder(AddinacountActivity.this).setTitle("系统信息")
.setMessage("增添收入成功,是否继续添加?")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO 自动生成的方法存根
money.setText("");
time.setText("");
handle.setText("");
beizhu.setText("");
type.setSelection(0);
return ;
}
}).setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO 自动生成的方法存根
Intent intent = new Intent(AddinacountActivity.this,FunctionActivity.class);
startActivity(intent);
finish();
}
}).show();
。。。。。。。