1、创建数据库和升级数据库
package org.jgw.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context)
{
super(context, "mxh.db", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table test(id integer primary key autoincrement,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("alter table test add memo varchar(50)");
}
}
2、数据库的添加、删除、修改、查询
package org.jgw.service;
import java.util.ArrayList;
import java.util.List;
import org.jgw.entity.testTable;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class testTableService
{
DBOpenHelper dbop;
SQLiteDatabase db;
public testTableService(Context context) {
dbop=new DBOpenHelper(context);
}
public void add(testTable t)
{
db=dbop.getWritableDatabase();
db.execSQL("insert into test(name,memo) values(?,?)",new Object[]{t.getName(),t.getMemo()});
}
public void delete(Integer id)
{
db=dbop.getWritableDatabase();
db.execSQL("delete from test where id=?",new Object[]{id});
}
public void update(testTable t)
{
db=dbop.getWritableDatabase();
db.execSQL("update test set name=?,memo=? where id=?",new Object[]{t.getName(),t.getMemo(),t.getId()});
}
public testTable find(Integer id)
{
testTable test;
db=dbop.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from test where id=?",new String[]{id.toString()});
if(cursor.moveToFirst())
{
test=new testTable(cursor.getInt(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("name")), cursor.getString(cursor.getColumnIndex("memo")));
cursor.close();
return test;
}
cursor.close();
return null;
}
public List<testTable> findSplitPage(int offset,int pagesize,String where)
{
testTable test;
List<testTable> list=new ArrayList<testTable>();
db=dbop.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from test "+where+" order by id limit ?,?",new String[]{offset+"",pagesize+""});
while(cursor.moveToNext())
{
String name=cursor.getString(cursor.getColumnIndex("name"));
String memo=cursor.getString(cursor.getColumnIndex("memo"));
test= new testTable(name, memo);
list.add(test);
}
cursor.close();
return list;
}
public int getSumCount()
{
db=dbop.getWritableDatabase();
Cursor cursor=db.rawQuery("select count(*) from test",null);
cursor.moveToFirst();
return cursor.getInt(0);
}
/*
* 事务的使用
*/
public void Transaction()
{
db=dbop.getWritableDatabase();
db.beginTransaction();
try
{
db.execSQL("update test set name=? where id=?",new Object[]{"sdfsdf","1"});
db.execSQL("update test set name=? where id=?",new Object[]{"aaaa","2"});
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
}
}
3、数据库实务操作
/*
* 事务的使用
*/
public void Transaction()
{
db=dbop.getWritableDatabase();
db.beginTransaction();
try
{
db.execSQL("update test set name=? where id=?",new Object[]{"sdfsdf","1"});
db.execSQL("update test set name=? where id=?",new Object[]{"aaaa","2"});
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
}
*/
public void Transaction()
{
db=dbop.getWritableDatabase();
db.beginTransaction();
try
{
db.execSQL("update test set name=? where id=?",new Object[]{"sdfsdf","1"});
db.execSQL("update test set name=? where id=?",new Object[]{"aaaa","2"});
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
}