SQLite说没有这样的专栏

时间:2013-12-06 09:38:57

标签: android database sqlite

DBAdapter.java

public void onCreate(SQLiteDatabase db) {

    db.execSQL(" CREATE TABLE " + DATABASE_TABLE + " (" + KEY_FIRSTNAME
                            + " TEXT NOT NULL, " + KEY_MIDDLENAME + " TEXT NOT NULL, "
                            + KEY_LASTNAME + " TEXT NOT NULL, " + KEY_USERNAME
                            + " TEXT NOT NULL, " + KEY_MAIL_ID + " TEXT NOT NULL, "
                            + KEY_PASSWORD + " TEXT NOT NULL, " + KEY_CONFIRM
                            + " TEXT NOT NULL, " + KEY_DATE_OF_BIRTH
                            + " INTEGER NOT NULL " + KEY_COUNTRY + " TEXT NOT NULL "
                            + ");");

}


public String getSinlgeEntry(String userName) {

    Cursor cursor = mDB.query(DATABASE_TABLE,null,"KEY_USERNAME=?",new String[]{userName},null,null,null);
    if (cursor.getCount() < 1) // UserName Not Exist
        return "NOT EXIST";
    cursor.moveToFirst();
    String password = cursor.getString(cursor.getColumnIndex("KEY_PASSWORD"));
    return password;

}

MainActivity.java

dbadapter = new DBAdapter(MainActivity.this);
dbadapter.open();
String stored_password = dbadapter.getSinlgeEntry(username);

if(password.equals(stored_password))
{
    Toast.makeText(MainActivity.this,"It is logging in",Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(MainActivity.this,Profile_view.class);
    startActivity(intent);

}

logcat的:

12-06 14:52:20.213: E/AndroidRuntime(9431): FATAL EXCEPTION: main
12-06 14:52:20.213: E/AndroidRuntime(9431): android.database.sqlite.SQLiteException: no such column: KEY_USERNAME: , while compiling: SELECT * FROM Saturday_Table_1129 WHERE KEY_USERNAME=?
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:147)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:368)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:131)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:95)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1534)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1414)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1370)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1450)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at com.example.signup.DBAdapter.getSinlgeEntry(DBAdapter.java:175)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at com.example.signup.MainActivity$1.onClick(MainActivity.java:55)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.view.View.performClick(View.java:3100)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.view.View$PerformClick.run(View.java:11644)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.os.Handler.handleCallback(Handler.java:587)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.os.Looper.loop(Looper.java:126)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at android.app.ActivityThread.main(ActivityThread.java:3997)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at java.lang.reflect.Method.invoke(Method.java:491)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-06 14:52:20.213: E/AndroidRuntime(9431):     at dalvik.system.NativeStart.main(Native Method)
12-06 14:52:28.734: I/Process(9431): Sending signal. PID: 9431 SIG: 9

我正在验证数据库中的用户名和密码。当我编译时,它说没有这样的列,但我已经定义了KEY_USERNAME列。数据库具有我尝试用于验证的用户名,但它不起作用。将非常感谢帮助。

4 个答案:

答案 0 :(得分:4)

用这个改变你的:

mDB.query(DATABASE_TABLE, null, KEY_USERNAME + "= ?", new String[] {userName});

您已在变量KEY_USERNAME中声明了列名,但您将其添加为字符串文字(并将其翻译为文本 KEY_USERNAME ),而不是包含其他文本的变量。这是例外的原因。

<强>更新

你在这里犯了同样的错误:

cursor.getString(cursor.getColumnIndex("KEY_PASSWORD"));

在这里你再次将常量添加为String文字,你需要以与上面相同的方式更改它:

cursor.getColumnIndex(KEY_PASSWORD)

现在它应该正常工作。

答案 1 :(得分:3)

您正在使用常量KEY_USERNAME创建表,但稍后在sql语句中使用字符串"KEY_USERNAME=?"

您可能希望在sql语句中使用常量,如下所示:KEY_USERNAME+"=?"

答案 2 :(得分:0)

很简单,您只需要使用您创建的参数,而不是像这样添加引号:

 Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME+" =?",new String[]{userName},null,null,null);

答案 3 :(得分:0)

您的查询错误您已写过“KEY_USERNAME =?”这是错误的

public String getSinlgeEntry(String userName) {

                Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME + " + "= ?",new String[]{userName},null,null,null);
                if (cursor.getCount() < 1) // UserName Not Exist
                    return "NOT EXIST";
                cursor.moveToFirst();
                String password = cursor.getString(cursor.getColumnIndex("KEY_PASSWORD"));
                return 
}

检查这是否有效