使用synchronized,却一直不很明白,是否已经做到同步绝对没问题了?
/*
* 类介绍:数据库帮助
*
* 说明:参考http://bbs.51cto.com/thread-990260-1.html
* 如果多线程同时读写,保持单个SqliteOpenHelper实例,同时对所有数据库操作的方法添加synchronized关键字。
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String TAG = "DatabaseHelper";
private static DatabaseHelper instance;
/**
* 构造
* @param context
*/
private DatabaseHelper(Context context) {
super(context, DatabaseBean.DB_NAME, null, DatabaseBean.DB_VERSION);
}
/**
* 获得单实例
* @param context
* @return
*/
public synchronized static DatabaseHelper the(Context context) {
if (DatabaseHelper.instance == null) {
DatabaseHelper.instance = new DatabaseHelper(context);
}
return DatabaseHelper.instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DatabaseBean.DB_CREATE_TABLE_MSGBOX);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/**
* 总数
* @return
*/
public synchronized long getCount() {
long count = 0;
try {
SQLiteDatabase db = getReadableDatabase();
String sql = "SELECT COUNT(*) FROM " + DatabaseBean.DB_TABLE_MSGBOX;
SQLiteStatement sqLiteStatement = db.compileStatement(sql);
count = sqLiteStatement.simpleQueryForLong();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return count;
}
/**
* 查询
* @param index
* @return
*/
public synchronized DatabaseBean query(int index) {
DatabaseBean databaseBean = new DatabaseBean();
try {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.query(DatabaseBean.DB_TABLE_MSGBOX, null, null, null, null, null, null);
if (cursor != null) {
if (cursor.moveToPosition(index)) {
int id = cursor.getInt(DatabaseBean.COLUMN_IDX_ID);
String datatime = cursor.getString(DatabaseBean.COLUMN_IDX_DATETIME);
String content = cursor.getString(DatabaseBean.COLUMN_IDX_CONTENT);
databaseBean.setId(id);
databaseBean.setDatatime(datatime);
databaseBean.setContent(content);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return databaseBean;
}
/**
* 插入
* @param datetime
* @param content
* @return
*/
public synchronized long insert(String datetime, String content) {
try {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseBean.COLUMN_DATETIME, datetime);
contentValues.put(DatabaseBean.COLUMN_CONTENT, content);
long count = db.insert(DatabaseBean.DB_TABLE_MSGBOX, null, contentValues);
return count;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return -1;
}
/**
* 删除
* @param id
* @return
*/
public synchronized int delete(int id) {
int count = -1;
try {
SQLiteDatabase db = getWritableDatabase();
String[] whereArgs = {""+id};
count = db.delete(DatabaseBean.DB_TABLE_MSGBOX, DatabaseBean.WHERECLAUSE, whereArgs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return count;
}
}
/**
* 类介绍:数据库数据区定义
*/
public class DatabaseBean {
public static final String DB_NAME = "avc.db";
public static final int DB_VERSION = 1;
public static final String DB_TABLE_MSGBOX = "msgbox";
public static final String COLUMN_ID = "id";
public static final String COLUMN_DATETIME = "datetime";
public static final String COLUMN_CONTENT = "content";
public static final String[] COLUMNS = {
COLUMN_ID, COLUMN_DATETIME, COLUMN_CONTENT
};
public static final int COLUMN_IDX_ID = 0;
public static final int COLUMN_IDX_DATETIME = 1;
public static final int COLUMN_IDX_CONTENT = 2;
//最后COLUMNS没有逗号
public static final String DB_CREATE_TABLE_MSGBOX = "create table if not exists " + DB_TABLE_MSGBOX + "("
+ COLUMN_ID + " integer primary key autoincrement,"
+ COLUMN_DATETIME + " varchar(20),"
+ COLUMN_CONTENT + " varchar(120)"
+ ")";
public static final String WHERECLAUSE = COLUMN_ID + "=?";
private int id;
private String datatime;
private String content;
/**
* 构造
*/
public DatabaseBean() {
super();
id = 0;
datatime = "";
content = "";
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the datatime
*/
public String getDatatime() {
return datatime;
}
/**
* @param datatime the datatime to set
*/
public void setDatatime(String datatime) {
this.datatime = datatime;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
}