146 percent Russian
146 percent Russian

Reputation: 2096

Android SQLite is empty after inserting ContentValues

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!

Upvotes: 1

Views: 822

Answers (1)

Lucifer
Lucifer

Reputation: 29632

I believe you are getting exception because You forgot to add a single space in following of your columns,

+ USERTIME + "INT,"+ UGAMETYPE + "INT"+

I suggest you to updated your that line with following line,

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"+");";

Note : After doing this change, you simply remove your old application from the device/emulator and then try again. The actual reason behind this is that your create table query is going to execute in onCreate() method. So to call this you must uninstall previous application.

Upvotes: 2

Related Questions