2016-02-05 80 views
0

我的SQLite包下的所有文件都給出錯誤。由於這些是預定義的類,不需要或不允許編輯,所以我很難做出我的項目。SQLiteDatabse給出意外錯誤

代碼太長,無法在此處發佈。 例如,這些進口SQLiteDatabase.java都出現在紅:

import android.database.sqlite.SQLiteDebug.DbStats; 
import dalvik.system.CloseGuard; 

和我的logcat顯示以下錯誤:

02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: FATAL EXCEPTION: main 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: android.database.sqlite.SQLiteException: no such table: user (code 1): , while compiling: select * from user 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at com.example.abcd.helloworld.DatabaseHelper.insertUser(DatabaseHelper.java:40) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at com.example.abcd.helloworld.SignUp$1.onClick(SignUp.java:52) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.view.View.performClick(View.java:4240) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.view.View$PerformClick.run(View.java:17721) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:730) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:137) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5103) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at java.lang.reflect.Method.invokeNative(Native Method) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:525) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:  at dalvik.system.NativeStart.main(Native Method) 

所有編碼由我做似乎是正確的。

爲表創建的代碼是:

public class DatabaseHelper extends SQLiteOpenHelper { 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "user.db"; 
    private static final String TABLE_NAME = "user"; 
    private static final String COLUMN_ID = "id"; 
    private static final String COLUMN_NAME = "name"; 
    private static final String COLUMN_EMAIL = "email"; 
    private static final String COLUMN_UNAME = "uname"; 
    private static final String COLUMN_PASS = "pass"; 
    SQLiteDatabase db; 
    private static final String TABLE_CREATE = "create table user (id integer primary key autoincrement," + 
      "name text, email text, uname text, pass text);"; 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(TABLE_CREATE); 
     this.db = db; 
    } 

    public void insertUser(User u) { 
     db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 

     String query = "select * from user"; 
     Cursor cursor = db.rawQuery(query, null); 
     int count = cursor.getCount(); 
     values.put(COLUMN_ID, count); 
     values.put(COLUMN_NAME, u.getName()); 
     values.put(COLUMN_EMAIL, u.getEmail()); 
     values.put(COLUMN_UNAME, u.getUname()); 
     values.put(COLUMN_PASS, u.getPass()); 

     db.insert(TABLE_NAME, null, values); 

    } 

    public String searchPass(String uname) { 
     db = this.getReadableDatabase(); 
     String query = "select uname, pass from" + TABLE_NAME; 
     Cursor cursor = db.rawQuery(query, null); 
     String a, b; 
     b = "not found"; 
     if (cursor.moveToFirst()) { 
      do { 
       a = cursor.getString(0); 

       if (a.equals(uname)) { 
        b = cursor.getString(1); 
        break; 
       } 
      } 
      while (cursor.moveToNext()); 
     } 
     db.close(); 
     return b; 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     String query = "DROP TABLE IF EXISTS" + TABLE_NAME; 
     db.execSQL(query); 
     this.OnCreate(db); 
    } 

    private void OnCreate(SQLiteDatabase db) { 
     db.execSQL(TABLE_CREATE); 
     this.db = db; 
    } 

} 
+1

不創建表,發表您對創建表的代碼 –

+0

確保名爲'user'在數據庫中存在該表.. – ELITE

+0

例外本身說,'沒有這樣的表:用戶(代碼1):'... – ELITE

回答

-1

您的代碼似乎是確定,嘗試調試SQLite的代碼。執行android代碼之外的語句。打開你的終端並執行sql語句,之後執行.tables,這個命令將顯示你的db中已有的表。

更多信息,你可以在這裏找到, http://www.tutorialspoint.com/sqlite/sqlite_create_table.htm

0

你在你的代碼中使用onCreate方法的兩倍。刪除重複。

試試下面的代碼:

public class DBHandler extends SQLiteOpenHelper { 

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

    public DBHandler(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String query = "create table user(" + 
       "id integer primary key autoincrement, " + 
       "name text, "+ 
       "email text, "+ 
       "uname text, "+ 
       "pass text"+ 
       ");"; 

     db.execSQL(query); 
    } 

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

     String query = "drop table if exists user"; 
     db.execSQL(query); 

     onCreate(db); 
    } 

    /* 

     Remaining code 

     Please do not add onCreate and onUpgrade method anymore. 

    */ 

}