Android:SQLite数据库“不存在这样的表”

时间:2014-09-02 15:40:09

标签: android sqlite

到目前为止,Android开发对我来说似乎是一种错误的错误,所以我再次回答一个问题。 我正在通过SQLite创建数据库连接,但是我收到以下错误:

(1) no such table: TABLE_NAME

我尝试重新安装我的应用,重命名应用必须创建的.db文件以及我在网上找到的其他方法,但都没有用。总而言之,我已经坚持了几天。

我的代码:

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setGlobalContext(this);
    SQL = new SQLLib(this);

    Button LoginBtn = (Button)findViewById(R.id.login_btn);
    LoginBtn.setOnClickListener(this);
    Button RegisterBtn = (Button)findViewById(R.id.register_btn);
    RegisterBtn.setOnClickListener(this);

    newTab("Tag1", R.id.login, "Login");
    newTab("Tag2", R.id.newitem, "Nieuw");
    newTab("Tag3", R.id.viewitems, "Overzicht");

    getTabHost().setCurrentTab(0);
    hideTab(1);
    hideTab(2);
}

登录功能 - 附加到LoginBtn

public void Login(){
    if(Username.replaceAll(" ", "") != "" && Password.replaceAll(" ", "") != ""){
        if(SQL.SelectString("SELECT PASSWORD FROM USERLOGIN WHERE USERNAME = '"+Username+"'") == Password){
            Message("Login");
        }else{
            Message(SQL.SelectString("SELECT PASSWORD FROM USERLOGIN WHERE USERNAME = '"+Username+"'")+", "+Password);
        }
    }else{
        Message("Not all data is inserted. Try again!");
    }
}

注册功能 - 附于RegisterBtn

public void Register(){
    if(Username.replaceAll(" ", "") != "" && Password.replaceAll(" ", "") != ""){
        if(SQL.SelectString("SELECT USERNAME FROM USERLOGIN WHERE USERNAME = '" + Username+"'") == ""){
            SQL.Insert("USERLOGIN", new String[]{"USERNAME", "PASSWORD"}, new String[]{"\""+Username+"\"", "\""+Password+"\""});
        }else{
            Message("User'"+Username+"' already exists!");
        }
    }else{
        Message("Not all data is inserted. Try again!");
    }
}

SQLLIB

private static final String DATABASE_NAME = "pwmanagement.db";
private static final int DATABASE_VERSION = 1;

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

@Override
public void onCreate(SQLiteDatabase db) {   
    db.rawQuery("CREATE TABLE userlogin (_id INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT, PASSWORD TEXT);", null);
    db.rawQuery("CREATE TABLE passwords (_id INTEGER PRIMARY KEY AUTOINCREMENT, USERLOGIN INTEGER,  USERNAME TEXT, PASSWORD TEXT);", null);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void Insert(String table, String[] columns, String[] contents){
    SQLiteDatabase db = this.getWritableDatabase();
    if(columns.length == contents.length){
        ContentValues values = new ContentValues();

        for(int i = 0; i < contents.length; i++){
            values.put(columns[i], contents[i]);
        }

        db.insert(table, null, values);
    }

    db.close();
}

public String SelectString(String query){
    SQLiteDatabase db = this.getReadableDatabase();
    String s = "";
    Cursor cursor;

    cursor = db.rawQuery(query, null);

    while(cursor.moveToNext()){
        s = cursor.getString(0);
    }

    db.close();
    return s.replaceAll(" ", "");
}

错误的完整LogCat

09-02 17:28:23.485: E/SQLiteLog(7569): (1) no such table: USERLOGIN
09-02 17:28:23.495: E/AndroidRuntime(7569): FATAL EXCEPTION: main
09-02 17:28:23.495: E/AndroidRuntime(7569): Process: com.example.password, PID: 7569
09-02 17:28:23.495: E/AndroidRuntime(7569): android.database.sqlite.SQLiteException: no such table: USERLOGIN (code 1): , while compiling: SELECT USERNAME FROM USERLOGIN WHERE USERNAME = 'steven'
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:923)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:534)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:65)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1370)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1309)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at com.example.password.SQLLib.SelectString(SQLLib.java:62)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at com.example.password.MainActivity.Register(MainActivity.java:85)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at com.example.password.MainActivity.onClick(MainActivity.java:50)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.view.View.performClick(View.java:4470)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.view.View$PerformClick.run(View.java:18593)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.os.Handler.handleCallback(Handler.java:733)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.os.Looper.loop(Looper.java:157)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at android.app.ActivityThread.main(ActivityThread.java:5867)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at java.lang.reflect.Method.invokeNative(Native Method)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at java.lang.reflect.Method.invoke(Method.java:515)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
09-02 17:28:23.495: E/AndroidRuntime(7569):     at dalvik.system.NativeStart.main(Native Method)

所以,我的问题是: 这个错误来自何处以及如何解决?我按照我在网上找到的步骤,甚至是我拥有的书籍。

1 个答案:

答案 0 :(得分:3)

您使用了错误的指令,并且您没有创建表格:

 db.rawQuery("CREATE TABLE...

应该是:

 db.execSQL("CREATE TABLE... 

rawQuery()仅用于执行 SELECT 语句(SQL 查询)。没有其他的。
所有其他SQL 命令都是通过execSQL()

执行的