views:

184

answers:

2

Hi, i am having this problem with the android database. I adopted the DBAdapter file the NotepadAdv3 example from the google android page.

DBAdapter.java

public class DBAdapter {
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "PasswordDb";
private static final String DATABASE_TABLE = "myuserdata";
private static final String DATABASE_USERKEY = "myuserkey";
private static final int DATABASE_VERSION = 2;

public static final String KEY_USERKEY = "userkey";
public static final String KEY_TITLE = "title";
public static final String KEY_DATA = "data";
public static final String KEY_ROWID = "_id";

private final Context mContext;

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DB_CREATE_KEY =
    "create table " + DATABASE_USERKEY
    + " ("
    + "userkey text not null"
    +");";

private static final String DB_CREATE_DATA =
    "create table " + DATABASE_TABLE
    + " ("
    + "_id integer primary key autoincrement, "
    + "title text not null"
    + "data text"
    +");";

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
        public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DB_CREATE_KEY);
        db.execSQL(DB_CREATE_DATA);
    }

    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
            + " to "
            + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS myuserkey");
        db.execSQL("DROP TABLE IF EXISTS myuserdata");            
        onCreate(db);
    }
}

public DBAdapter(Context ctx)
{
    this.mContext = ctx;
}

public DBAdapter Open() throws SQLException{    
    try {
        mDbHelper = new DatabaseHelper(mContext); 
    }
    catch(Exception e){
        Log.e(TAG, e.toString());
    }
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close(){
    mDbHelper.close();
}

public Long storeKey(String userKey){
    ContentValues initialValues = new ContentValues();

    initialValues.put(KEY_USERKEY, userKey);
    try {
        mDb.delete(DATABASE_USERKEY, "1=1", null);
    }
    catch(Exception e)
    {
        Log.e(TAG, e.toString());
    }
    return mDb.insert(DATABASE_USERKEY, null, initialValues);
}

public String retrieveKey() {   
    final Cursor c;
    try {
        c = mDb.query(DATABASE_USERKEY, new String[] {
            KEY_USERKEY}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }catch(Exception e){
        Log.e(TAG, e.toString());
        return "";
    }

    if(c.moveToFirst()){
        return c.getString(0);
    }
    else{
        Log.d(TAG, "UserKey Empty");
    }
    return "";
}
//not including any function related to "myuserdata" table

}

Class1.java

{
mUserKey = mDbHelper.retrieveKey();
mDbHelper.storeKey(Key);

}

the error that i am receiving is from Log.e(TAG, e.toString()) in the methods retrieveKey() and storeKey()

"no such table: myuserkey: , while compiling: SELECT userkey FROM myuserkey"

A: 

i think you are not mention your provider tag in AndroidManifest.xml .Check it.

u have to mention the tag like

 <provider android:name=".name of providerclass"
          android:authorities="authority path" />
Praveen Chandrasekaran
i think we need 'provider' only when we have to share the data between applications(http://developer.android.com/guide/topics/providers/content-providers.html). In my case i am storing the data in my application only
Vamsi
A: 

Did you pop the DB version so onUpgrade fires? You're at version 2 in the example above but if you changed the schema since version 2 then you need to pop the version again.

Brad Hein
i didnt changed the database version. can you explain me little bit more about popping/re-popping of db version?
Vamsi
The fact that your code is referring to a table in the database which does not exist, suggests your DB was defined with a schema that did not include that table AND you did not increase the DB version after you changed the code to define that table. Simply increase DB_VERSION and re-run.
Brad Hein
very thanks, it really worked.
Vamsi
Great! Always remember to change the DB version any time you change the table definition in the code. One trick I use is to keep track of the changes I made, along with the version number, in a comments section at the top of the class.
Brad Hein