public void open() {
if (null == db) {
db = mDbHelper.getDatabase();
}
} public void close() {
mDbHelper.closeDatabase();
db = null;
} public long insert(UserIDTable uit) {
try {
open();
ContentValues initialValues = new ContentValues();
initialValues.put(Column_UserID, uit.userID);
long count = db.insert(DATABASE_TABLE, null, initialValues);
if (-1 == count) {
Log.e(TAG, "insert() failed");
}
close();
return count;
} catch (Exception e) {
Log.e(TAG, "insert() Exception=" + e);
}
return -1;
}
public int delete(int id) {
try {
open();
String cursorID = Column_ID + "=" + id;
int count = db.delete(DATABASE_TABLE, cursorID, null);
if (0 == count) {
Log.e(TAG, "delete() failed");
}
close();
return count;
} catch (Exception e) {
Log.e(TAG, "delete() Exception=" + e);
}
return -1;
} public int update(UserIDTable uit) {
try {
open();
ContentValues initialValues = new ContentValues(); initialValues.put(Column_UserID, uit.userID);
String cursorID = Column_ID + "=" + uit.ID;
int count = db.update(DATABASE_TABLE, initialValues, cursorID, null);
if (0 == count) {
Log.e(TAG, "update() failed");
}
close();
return count;
} catch (Exception e) {
Log.e(TAG, "update() Exception=" + e);
}
return -1;
} public UserIDTable getFirst() {
try {
open();
Cursor cursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (null == cursor) {
close();
return null;
} UserIDTable uit = null;
while (cursor.moveToNext()) {
uit = new UserIDTable();
uit.ID = cursor.getInt(cursor.getColumnIndex(Column_ID));
uit.userID = cursor.getString(cursor.getColumnIndex(Column_UserID));
break;
}
cursor.close(); close();
Log.d(TAG, "getFirst()=" + uit);
return uit;
} catch (Exception e) {
Log.e(TAG, "getFirst() Exception=" + e);
}
return null;
}
public void init() {
UserIDTable uit = new UserIDTable();
uit.ID = -1;
uit.userID = "default_uid";
long iRet = insert(uit);
if (-1 == iRet) {
Log.e(TAG, "init() error");
}
else {
Log.d(TAG, "init() ok");
}
}
}public class UserIDTable {
public int ID;
public String userID; @Override
public String toString() {
return "UserIDTable [id=" + ID
+ ", userID=" + userID
+ "]";
}
}
if (null == db) {
db = mDbHelper.getDatabase();
}
} public void close() {
mDbHelper.closeDatabase();
db = null;
} public long insert(UserIDTable uit) {
try {
open();
ContentValues initialValues = new ContentValues();
initialValues.put(Column_UserID, uit.userID);
long count = db.insert(DATABASE_TABLE, null, initialValues);
if (-1 == count) {
Log.e(TAG, "insert() failed");
}
close();
return count;
} catch (Exception e) {
Log.e(TAG, "insert() Exception=" + e);
}
return -1;
}
public int delete(int id) {
try {
open();
String cursorID = Column_ID + "=" + id;
int count = db.delete(DATABASE_TABLE, cursorID, null);
if (0 == count) {
Log.e(TAG, "delete() failed");
}
close();
return count;
} catch (Exception e) {
Log.e(TAG, "delete() Exception=" + e);
}
return -1;
} public int update(UserIDTable uit) {
try {
open();
ContentValues initialValues = new ContentValues(); initialValues.put(Column_UserID, uit.userID);
String cursorID = Column_ID + "=" + uit.ID;
int count = db.update(DATABASE_TABLE, initialValues, cursorID, null);
if (0 == count) {
Log.e(TAG, "update() failed");
}
close();
return count;
} catch (Exception e) {
Log.e(TAG, "update() Exception=" + e);
}
return -1;
} public UserIDTable getFirst() {
try {
open();
Cursor cursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (null == cursor) {
close();
return null;
} UserIDTable uit = null;
while (cursor.moveToNext()) {
uit = new UserIDTable();
uit.ID = cursor.getInt(cursor.getColumnIndex(Column_ID));
uit.userID = cursor.getString(cursor.getColumnIndex(Column_UserID));
break;
}
cursor.close(); close();
Log.d(TAG, "getFirst()=" + uit);
return uit;
} catch (Exception e) {
Log.e(TAG, "getFirst() Exception=" + e);
}
return null;
}
public void init() {
UserIDTable uit = new UserIDTable();
uit.ID = -1;
uit.userID = "default_uid";
long iRet = insert(uit);
if (-1 == iRet) {
Log.e(TAG, "init() error");
}
else {
Log.d(TAG, "init() ok");
}
}
}public class UserIDTable {
public int ID;
public String userID; @Override
public String toString() {
return "UserIDTable [id=" + ID
+ ", userID=" + userID
+ "]";
}
}
解决方案 »
- Camera sensor 调试遇到问题
- //求教有关蓝牙的问题,急!!
- 以下是Ubuntu 下执行命令行 ./grxmlcompile -par /home/SREC/baseline11k.par -grxml digits.g
- 当前Activity不能dismiss掉Activity中的dialog!!急!!!
- android cursor 问题
- res文件夹添加了新项,R却没变
- android上sqlite数据库打开出错
- android ndk移植第三方库错误
- android adapter里面怎么使用startActivityForResult()方法
- android OkHttpUtils网络延迟多次请求接口
- 求助。。这个问题找了很久,还是没有找到解决的方法。。
- android 播放socket发送的视频流问题
public class DataBaseHelper extends SQLiteOpenHelper {
static final String TAG = "DataBaseHelper"; public static DataBaseHelper mDataBaseHelper;
private final static String DATABASE_NAME = "data.db";
public final static int DATABASE_VERSION = 1;
static SQLiteDatabase db;
private AtomicInteger mOpenCounter = new AtomicInteger();
private static DataBaseHelper sInstance;
private static Context mContext;
public static DataBaseHelper getInstance(Context context) {
if (null == sInstance) {
synchronized(DataBaseHelper.class) {
if (null == sInstance) {
mContext = context;
sInstance = new DataBaseHelper(context);
}
}
}
return sInstance;
} private DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
} protected synchronized SQLiteDatabase getDatabase() {
if (1 == mOpenCounter.incrementAndGet()) {
db = getWritableDatabase();
}
return db;
}
if (0 == mOpenCounter.decrementAndGet()) {
db.close();
db = null;
}
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(UserIDDB.createTableSQL);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
int version = oldVersion;
if(oldVersion == 1) {
db.execSQL(UserIDDB.createTableSQL);
initUseridDB(mContext, db);
}
} public void initUseridDB(Context context, SQLiteDatabase db) {
UserIDTable uit = new UserIDTable();
uit.ID = -1;
uit.userID = UserConfigTable.DEFAULT_USER_ID; try {
ContentValues initialValues = new ContentValues();
initialValues.put(UserIDDB.Column_UserID, uit.userID);
long count = db.insert(UserIDDB.DATABASE_TABLE, null, initialValues);
if (-1 == count) {
Log.e(TAG, "initUseridDB() failed");
}
else {
Log.d(TAG, "initUseridDB() ok");
}
return;
} catch (Exception e) {
Log.e(TAG, "initUseridDB() Exception=" + e);
}
}