BodhiByte
BodhiByte

Reputation: 209

Android database: insert query error

I am developing an application requiring persistence functionality.From my limited understanding two classes are required for this - SQLiteHelper class & DatabaseManager class.

To get things moving, I have a simple process that will take a single text input and insert it into the database. However, I am receiving an error:

12-29 19:17:47.952: E/SQLiteDatabase(5840): android.database.sqlite.SQLiteException: no such table: UserTable (code 1): , while compiling: INSERT INTO UserTable(username) VALUES (?)

Implying there is no user table, although when onCreate() is called, this table should be created.

I would be really greatful for any assistance in helping to overcome this obstacle. Below are the two classes used in creating the database functionality. Many thanks.

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME         = "NutrtionIntuition";
private static final int  DATABASE_VERSION    = 2;

// Database creation sql statement
private static final String DATABASE_CREATE =   "create table UserTable " +
                                                "( _id integer primary key,name text not null);";

public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Method is called during creation of the database. Method is also called on upgrade of database?
@Override
public void onCreate(SQLiteDatabase database) {
            database.execSQL(DATABASE_CREATE);
}

// Method is called during an upgrade of the database,
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

                database.execSQL("DROP TABLE IF EXISTS User");
                onCreate(database);
} }
public class DatabaseManager{  

    private DatabaseHelper      dbHelper;  
    private SQLiteDatabase      database;  

    private static final String     USER_TABLE          = "UserTable";

    public static final String      KEY_ROWID           = "_id";
    public static final String      KEY_USERNAME        = "username";


    public DatabaseManager(Context context){  
      dbHelper = new DatabaseHelper(context);  
      database = dbHelper.getWritableDatabase();  
    }

    //Insert Records into the User Table
    public long insertRecords(int id, String name){  
        ContentValues values = new ContentValues();  
        //values.put(KEY_ROWID , id);  
        values.put(KEY_USERNAME, name); 
        Log.v("Test","Inserting.....");

        return database.insert(USER_TABLE, null, values);  
    }   

    public Cursor selectRecords() {
         String[] cols = new String[] {KEY_ROWID, KEY_USERNAME};  
         Cursor mCursor = database.query(true, USER_TABLE,cols,null  
              , null, null, null, null, null);  
         if (mCursor != null) {  
          mCursor.moveToFirst();  
       }  
       return mCursor; // iterate to get each value.
    }
}

Upvotes: 0

Views: 110

Answers (1)

Sush
Sush

Reputation: 3874

You're creating a table with _id and name, while inserting you are using _id and username. One more - in onUpgrade, DROP TABLE IF EXISTS User should be DROP TABLE IF EXISTS UserTable

Upvotes: 0

Related Questions