2012-08-17 94 views
0

我試圖插入一些數據到我的SQLite數據庫。但我希望收到錯誤代碼1,說我的專欄不存在。有人可以幫我請了,下面是我的代碼:SQLite錯誤代碼= 1

08-17 13:12:46.473: I/Database(13328): sqlite returned: error code = 1, msg = table login has no column named uid 
08-17 13:12:46.503: E/Database(13328): Error inserting uid=502e7b90657ab1.86949026 created_at=2012-08-17 12:12:48 email=epaas name=hwheuu 
08-17 13:12:46.503: E/Database(13328): android.database.sqlite.SQLiteException: table login has no column named uid: , while compiling: INSERT INTO login(uid, created_at, email, name) VALUES(?, ?, ?, ?); 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41) 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1231) 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1658) 
08-17 13:12:46.503: E/Database(13328): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1515) 
08-17 13:12:46.503: E/Database(13328): at library.DatabaseHandler.addUser(DatabaseHandler.java:69) 
08-17 13:12:46.503: E/Database(13328): at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:135) 
08-17 13:12:46.503: E/Database(13328): at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:1) 
08-17 13:12:46.503: E/Database(13328): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
08-17 13:12:46.503: E/Database(13328): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
08-17 13:12:46.503: E/Database(13328): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
08-17 13:12:46.503: E/Database(13328): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
08-17 13:12:46.503: E/Database(13328): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
08-17 13:12:46.503: E/Database(13328): at java.lang.Thread.run(Thread.java:1027) 

下面是我的數據庫代碼:

package library; 

import java.util.HashMap; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHandler extends SQLiteOpenHelper { 

    // All Static variables 
    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "thryfting_api"; 

    // Login table name 
    private static final String TABLE_LOGIN = "login"; 

    // Login Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_NAME = "name"; 
    private static final String KEY_EMAIL = "email"; 
    private static final String KEY_UID = "uid"; 
    private static final String KEY_CREATED_AT = "created_at"; 

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

    // Creating Tables 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," 
       + KEY_NAME + " TEXT," 
       + KEY_EMAIL + " TEXT UNIQUE," 
       + KEY_UID + " INTEGER," 
       + KEY_CREATED_AT + " TEXT" + ")"; 
     db.execSQL(CREATE_LOGIN_TABLE); 
    } 

    // Upgrading database 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN); 

     // Create tables again 
     onCreate(db); 
    } 

    /** 
    * Storing user details in database 
    * */ 
    public void addUser(String name, String email, String uid, String created_at) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_NAME, name); // Name 
     values.put(KEY_EMAIL, email); // Email 
     values.put(KEY_UID, uid); // Email 
     values.put(KEY_CREATED_AT, created_at); // Created At 

     // Inserting Row 
     db.insert(TABLE_LOGIN, null, values); 
     db.close(); // Closing database connection 
    } 

    /** 
    * Getting user data from database 
    * */ 
    public HashMap<String, String> getUserDetails(){ 
     HashMap<String,String> user = new HashMap<String,String>(); 
     String selectQuery = "SELECT * FROM " + TABLE_LOGIN; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     // Move to first row 
     cursor.moveToFirst(); 
     if(cursor.getCount() > 0){ 
      user.put("name", cursor.getString(1)); 
      user.put("email", cursor.getString(2)); 
      user.put("uid", cursor.getString(3)); 
      user.put("created_at", cursor.getString(4)); 
     } 
     cursor.close(); 
     db.close(); 
     // return user 
     return user; 
    } 

    /** 
    * Getting user login status 
    * return true if rows are there in table 
    * */ 
    public int getRowCount() { 
     String countQuery = "SELECT * FROM " + TABLE_LOGIN; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     int rowCount = cursor.getCount(); 
     db.close(); 
     cursor.close(); 

     // return row count 
     return rowCount; 
    } 

    /** 
    * Re crate database 
    * Delete all tables and create them again 
    * */ 
    public void resetTables(){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     // Delete All Rows 
     db.delete(TABLE_LOGIN, null, null); 
     db.close(); 
    } 

} 
+0

從您的應用程序的信息做一個明確的數據或更改DATABASE_VERSION爲2,檢查 – nandeesh 2012-08-17 17:27:05

+0

打開數據庫,並檢查該列確實存在。 – Doomsknight 2012-08-17 17:29:43

+0

好吧我即將嘗試 – 2012-08-17 17:30:31

回答

3

的錯誤消息

login沒有名爲uid

表明你的數據庫是不是在你期望的狀態欄。您確實使用名爲uidKEY_UID)的列創建了該表,但在設備上已創建數據庫之後,您很可能已添加了該列。一旦創建完成,它會一直存在,直到您明確刪除該文件或卸載您的應用。在數據庫文件丟失之前,onCreate將不會再次執行。

您需要做的是在每次更改表定義時升級數據庫版本。這將觸發onUpgrade代碼,然後代碼將按照您定義的方式重新創建表格。

你的解決方法是最有可能只是

private static final int DATABASE_VERSION = 2; 
+1

它的工作!!!!! yessssss!謝謝 – 2012-08-17 17:38:05

+0

@EricOboite如果有幫助,請不要忘記接受答案。 – 2012-08-17 18:42:47

0

嘗試用

private static final String KEY_ID = "uid"; 

,而不是

private static final String KEY_ID = "id"; 
+2

錯誤說'表登錄沒有列命名uid:'所以他已經嘗試過,並有2列。一個UID和一個ID – Doomsknight 2012-08-17 17:29:03

+0

我只是試過它沒有工作 – 2012-08-17 17:29:06

相關問題