I'm writing a program. At some place I want to insert some values inside my local database. But if check the db after inserting it is empty. Here is the code
LeaderBase sqh = new LeaderBase(this);
SQLiteDatabase sqdb = null;
if (checkDataBase()){
sqdb = SQLiteDatabase.openDatabase("/data/data/com.example.test7/databases/leader_database.db", null, 0);
}
else{
}
sqdb = sqh.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(LeaderBase.USERNAME, String.valueOf(nameRes));
cv.put(LeaderBase.USERMONEY, moneyRes);
cv.put(LeaderBase.USERTIME, timeRes);
cv.put(LeaderBase.UGAMETYPE, gtype);
sqdb.insert(LeaderBase.TABLE_NAME, null, cv);
Cursor cursor2 = sqdb.query(LeaderBase.TABLE_NAME, new String[] {
LeaderBase._ID, LeaderBase.USERNAME },
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
int check_count = cursor2.getCount();
sqdb.close();
sqh.close();
The code of LeaderBase class:
public class LeaderBase extends SQLiteOpenHelper implements BaseColumns {
private static final String DATABASE_NAME = "leader_database.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "leader_board";
public static final String UID = "_id";
public static final String USERNAME = "username";
public static final String USERMONEY = "usermoney";
public static final String USERTIME = "usertime";
public static final String UGAMETYPE = "ugametype";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
+ TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ USERNAME + " VARCHAR(255),"
+ USERMONEY + " BIGINT,"
+ USERTIME + "INT,"
+ UGAMETYPE + "INT"+
");";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
+ TABLE_NAME;
public LeaderBase(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
The problem is that if I check the value check_count it is zero. And the database is empty. What's the problem?
Thanks for all the answers!