1.写一个类继承SQLiteOpenHelper
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, Const.DB_DBNAME , null, Const.DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Const.DB_CREATE_TABLE) ;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
2.定义一个Const类,定义数据库所需的常量
public class Const {
// 定义数据库相关常量
public static final int DB_VERSION = 1;
public static final String DB_DBNAME = "bwl.db";
public static final String DB_TABLENAME = "bwlinfo";
public static final String DB_COLUMN_ID = "_id";
public static final String DB_COLUMN_TITLE = "title";
public static final String DB_COLUMN_CONTENT = "content";
public static final String DB_COLUMN_DATE = "date";
public static final String DB_CREATE_TABLE = "CREATE TABLE " + DB_TABLENAME + "(" + DB_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + DB_COLUMN_TITLE + " text not null," + DB_COLUMN_CONTENT + " text," + DB_COLUMN_DATE + " text)" ;
// Intent的key常量
// SharedPreferences的Key常量 , 文件名
// 内部存储的文件名
}
3.写操作数据库的方法
// 封装数据库操作
public class DBTool {
private static MyHelper helper ;
private static SQLiteDatabase db ;
// insert
public static long insertData(Context context , BWL bwl) {
openConn(context);
ContentValues values = new ContentValues();
values.put(Const.DB_COLUMN_TITLE, bwl.getTitle());
values.put(Const.DB_COLUMN_CONTENT , bwl.getContent());
values.put(Const.DB_COLUMN_DATE, bwl.getDate());
long rowId = db.insert(Const.DB_TABLENAME, null, values);
closeConn();
return rowId;
}
// update
/**
* 更新记录时只能更新一条
* @param updateId 要更新的数据行id
*/
public static int updateData(Context context , BWL bwl , long updateId) {
openConn(context);
ContentValues values = new ContentValues();
values.put(Const.DB_COLUMN_TITLE, bwl.getTitle());
values.put(Const.DB_COLUMN_CONTENT , bwl.getContent());
values.put(Const.DB_COLUMN_DATE, bwl.getDate());
int result = db.update(Const.DB_TABLENAME, values, Const.DB_COLUMN_ID+"="+updateId, null) ;
closeConn();
return result;
}
/**
* 自定义更新,可以更新多条记录
* @param context
* @param bwl
* @param where 更新条件
* @return 更新的行数
*/
public static int updateData(Context context , BWL bwl , String where) {
openConn(context);
ContentValues values = new ContentValues();
values.put(Const.DB_COLUMN_TITLE, bwl.getTitle());
values.put(Const.DB_COLUMN_CONTENT , bwl.getContent());
values.put(Const.DB_COLUMN_DATE, bwl.getDate());
int result = db.update(Const.DB_TABLENAME, values, where, null) ;
closeConn();
return result;
}
// delete
/**
* 最多删除一行
* @param deleteId 要删除行的id
* @return 删除的行数
*/
public static int deleteData(Context context , long deleteId) {
openConn(context);
int result = db.delete(Const.DB_TABLENAME, Const.DB_COLUMN_ID+"="+deleteId, null);
closeConn();
return result;
}
/**
* 自定义删除
* @param context
* @param where 删除数据的条件
* @return 删除的行数
*/
public static int deleteData(Context context , String where) {
openConn(context);
int result = db.delete(Const.DB_TABLENAME, where, null);
closeConn();
return result;
}
/**
* 根据给定的id,查询该条记录
* @param context
* @param rowId 要查询的数据行id
* @return 如果rowId存在,查找成功,返回BWL对象,如果rowId不存在,返回null
*/
public static BWL selectOne(Context context , long rowId) {
openConn(context);
Cursor cursor = db.query(
Const.DB_TABLENAME , null,
Const.DB_COLUMN_ID+"="+rowId , null,
null, null, null);
BWL bwl = null;
if(cursor.moveToNext()) {
bwl = new BWL(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getString(3)) ;
}
closeConn();
return bwl;
}
/**
* 按照用户指定的条件进行查询
* @param context
* @param where 查询条件,如果为null,将查询所有数据
* @param order 要排序的列 , 如果为null,将按照默认主键排序
* @param isAsc 排序列的升降序
* @return 查询结果集,如果没有查找到任何数据,返回null
*/
// List<BWL> list = DBTool.selectMore(this, null, Const.DB_COLUMN_DATE, false);
public static List<BWL> selectMore(Context context , String where , String order , Boolean isAsc) {
openConn(context);
String orderStr = "";
if(null != order && order.trim().length() != 0) {
orderStr += order;
if(isAsc != null && !isAsc) {
orderStr += " desc";
}
}
Cursor cursor = db.query(
Const.DB_TABLENAME , null,
where , null,
null, null, orderStr);
if(cursor.getCount() <= 0) {
return null ;
}
List<BWL> list = new ArrayList<BWL>();
while(cursor.moveToNext()) {
list.add(new BWL(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getString(3))) ;
}
closeConn();
return list;
}
/**
* 执行sql语句
* @param context
* @param sql 要执行的sql语句
*/
public static void execCmd(Context context , String sql) {
openConn(context);
db.execSQL(sql);
closeConn();
}
// 打开连接
private static void openConn(Context context) {
if(null == db) {
if(null == helper) {
helper = new MyHelper(context);
}
db = helper.getWritableDatabase();
}
}
// 关闭连接
private static void closeConn() {
if(null != db) {
db.close();
db = null;
}
}
}
4.对数据库进行操作
BWL bwl = new BWL("星期六", "周末小聚", "2011年");
// DBTool.insertData(this, bwl);
//
// BWL bwl2 = new BWL("周末", "去玩", "2011年");
// DBTool.updateData(this, bwl2, 1);
//
// BWL bwl3 = new BWL("周五", "去玩", "2011年");
// DBTool.updateData(this, bwl2, Const.DB_COLUMN_TITLE +" like ‘%周%‘");
// BWL bwl4 = DBTool.selectOne(this, 2);
// if(null != bwl4) {
// Toast.makeText(this, bwl4.toString(), Toast.LENGTH_LONG).show();
// }
// List<BWL> list = DBTool.selectMore(this, null, Const.DB_COLUMN_DATE, false);
// String content = "";
// for (BWL bwl : list) {
// content += bwl.toString() + "\n";
// }
// Toast.makeText(this,content, Toast.LENGTH_LONG).show();
// DBTool.deleteData(this, 1);
// DBTool.deleteData(this, null);
Android Sqlite的操作,布布扣,bubuko.com