2012-06-12 66 views
0

應用程序與PHP類交互以從MySQL獲取數據並使用SQLite將用戶數據存儲在設備上,但它給了我錯誤消息。有人可以幫我解決嗎?Android SQLite表缺少一列

DatabaseHandler.java

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 = "sqliteData"; 

    // 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_ROLE = "role"; 

    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_ROLE + " 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 id, String name, String role) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_ID, id); // id 
     values.put(KEY_NAME, name); // Name 
     values.put(KEY_ROLE, role); // Role 

     // 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("id", cursor.getString(1)); 
      user.put("name", cursor.getString(2)); 
      user.put("role", cursor.getString(3)); 
     } 
     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(); 
    } 

} 

logcat的

E/JSON(373): {"tag":"login","success":1,"error":0,"id":"013","user":{"name":"Mike Burton","role":"Developer"}} 

I/Database(373): sqlite returned: error code = 1, msg = table login has no column named role 

E/Database(373): Error inserting id=013 role=Developer name=Mike Burton 

E/Database(373): android.database.sqlite.SQLiteException: table login has no column named role: , while compiling: INSERT INTO login(id, role, name) VALUES(?, ?, ?); 
+0

如果您在任何時候更改了「CREATE_LOGIN_TABLE」,則需要顯式刪除舊錶並創建新表。最簡單的方法之一是將1添加到'DATABASE_VERSION'。 – Sam

+0

你也有一個SQL注入'任何'或'1'='1'在密碼字段。 –

回答

2

這聽起來像你可能有modified.the模式,但也沒有增加的版本(所以DB沒有得到重建與您的更改)。如果是這種情況,您可以刪除/重新安裝您的應用程序或增加數據庫版本,以便onUpgrade被調用。

+0

明白了!它現在有效。謝謝! – tvdog

+0

@tvdog。大!如果你能接受那個很酷的答案(點擊複選標記)。這可以讓每個人都知道你的問題已經解決,並給予幫助你的人的功勞。 – Barak