简体   繁体   中英

“column does not exist” when creating db

I'm trying to create an android app with a simple db (using the built-in sqlite db). I have created a class that extends SQLiteOpenHelper:

public class DatabaseHandle extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "pass_db";
public static final String ALIAS_NAME = "name";
public static final String USER_NAME = "user";
public static final String PASSWORD = "password";
public static final String TABLE_NAME = "passwords";


public DatabaseHandle(Context context) {
    super(context, DATABASE_NAME, null, 2);

}

@Override
public void onCreate(SQLiteDatabase database) {
    Log.i("DatabaseHandle","inside onCreate");
    database.execSQL("CREATE TABLE "+ TABLE_NAME +" (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
            " "+ALIAS_NAME+" TEXT, " + " "+USER_NAME+" TEXT, "+PASSWORD+" TEXT);");
    ContentValues values = new ContentValues();
    values.put(ALIAS_NAME, "Bank");
    values.put(USER_NAME, "maya123");
    values.put(PASSWORD, "xyz123");
    database.insert(TABLE_NAME, ALIAS_NAME, values);
}

and trying to get this data with a SimpleCurserAdapter:

public class MainActivityPasswords extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.user_list);
    database = (new DatabaseHandle(this)).getWritableDatabase();

    passwordCursor = database.rawQuery("SELECT _id, name, password FROM passwords ORDER BY name", null);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            R.layout.row, 
            passwordCursor, 
            new String[]{DatabaseHandle.ALIAS_NAME, DatabaseHandle.USER_NAME, DatabaseHandle.PASSWORD},
            new int[]{R.id.alias, R.id.user_name, R.id.password}, 0);
    setListAdapter(adapter);
    registerForContextMenu(getListView());
}

(this is partial code ofcourse)

when running the code i get:

Java.lang.IllegalArgumentException: Column 'user' does not exist.

Why is that? i can see clearly that i have 'user' column when opening the db file itself.

Help!

You forgot to select the column user in your query

Change this

passwordCursor = database.rawQuery("SELECT _id, name, password FROM passwords ORDER BY name", null);

to this

passwordCursor = database.rawQuery("SELECT _id, name, user, password FROM passwords ORDER BY name", null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM