整理了工作中用到的数据库操作方法,并写了一个demo实现了SQlite数据库的增删改查。
demo地址:android数据库实践demo
先上效果图:
下面贴一些关键代码:
DBHelper:
public class DBHelper extends SQLiteOpenHelper { private final static String DB_NAME = "test.db"; private final static int TABLE_VERSION = 1; public DBHelper(Context context) { super(context, DB_NAME, null, TABLE_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { //创建表 sqLiteDatabase.execSQL("create table if not exists " + Constants.MESSAGE_TABLE_NAME + "(id integer primary key autoincrement, message text, time text)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
MessageDao:
public class MessageDao { private SQLiteDatabase db; public MessageDao(Context context){ DBHelper dbHelper = new DBHelper(context); db = dbHelper.getWritableDatabase(); } /** * 关闭数据库 */ public void closeDB(){ db.close(); } /** * 向表中插入数据 * * @param message 数据 */ public void insert(Message message) { db.beginTransaction(); try { db.execSQL("insert into " + Constants.MESSAGE_TABLE_NAME + " values (null, ?, ?)", new Object[]{message.getMessage(), message.getTime()}); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } /** * 清空表数据 */ public void clear() { db.delete(Constants.MESSAGE_TABLE_NAME, "", new String[]{}); } /** * 通过ID删除指定数据 * * @param id ID */ public void delete(int id){ db.delete(Constants.MESSAGE_TABLE_NAME, "id = ?", new String[]{String.valueOf(id)}); } /** * 通过ID更新数据 * * @param message 数据 * @param id ID */ public void update(Message message, int id) { ContentValues cv = new ContentValues(); cv.put("time", message.getTime()); db.update(Constants.MESSAGE_TABLE_NAME, cv, "id = ?", new String[]{String.valueOf(id)}); } /** * 查询 * * @return Message列表 */ public List<Message> query() { ArrayList<Message> messageList = new ArrayList<>(); Cursor c = queryCursor(Constants.MESSAGE_TABLE_NAME); while (c.moveToNext()) { Message message = new Message(); message.setMessage(c.getString(c.getColumnIndex("message"))); message.setTime(c.getString(c.getColumnIndex("time"))); message.setId(c.getInt(c.getColumnIndex("id"))); messageList.add(message); } c.close(); return messageList; } /** * 查询指针 * * @param tableName:表名 * @return Cursor */ public Cursor queryCursor(String tableName) { return db.rawQuery("select * from " + tableName, null); } }Message对象:
public class Message { private int id; private String message; private String time; public Message(){ } public Message(String message, String time){ this.message = message; this.time = time; } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } public void setTime(String time) { this.time = time; } public String getTime() { return time; } }使用Dao来对数据库进行操作:下面是具体的增删改查:
public void insert(){
Message message = new Message("消息", getCurrentTime()); dao.insert(message); // query(); }
public void delete(){
dao.delete(Integer.valueOf(etId.getText().toString())); // query(); }
public void update(){
Message message = new Message("新消息", getCurrentTime()); dao.update(message, Integer.valueOf(etId.getText().toString())); // query(); }
public void query(){
List<Message> messageList = dao.query(); dBadapter.setMessageList(messageList); }
在有了Dao来操作数据库之后,数据库的使用就变得特别简便。
最后推荐篇很好的blog,我是从这上面学习的: