创建db时“列不存在”

时间:2014-12-09 20:22:14

标签: java android sqlite illegalargumentexception

我试图使用简单的数据库创建一个Android应用程序(使用内置的sqlite数据库)。 我创建了一个扩展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);
}

并尝试使用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());
}

(这是部分代码)

运行代码时我得到:

  

Java.lang.IllegalArgumentException:Column' user'不存在。

为什么?我可以清楚地看到我有“用户”#39;打开db文件本身时的列。

帮助!

1 个答案:

答案 0 :(得分:2)

您忘记在查询中选择列用户

更改此

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

到这个

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