2013-04-17 84 views
0

我添加了2個新列到我的SQL lite數據庫中,但我得到一個SQL錯誤,因爲其中一個新列沒有被創建。我讀過類似錯誤的其他線程,並進行雙重檢查以確保我的拼寫正確。Android數據庫SQLite異常「表沒有列命名」錯誤

這個問題似乎在我的onCreate,因爲這是程序崩潰的地方,但我似乎無法弄清楚。我添加了Player_Number和Team列,但是Player_Number並沒有被創建,而是一個團隊。

這裏是我的代碼將對DBAdapter:

public class PlayerDbAdapter { 

private static final String DATABASE_CREATE = "CREATE TABLE Players (_id integer primary key autoincrement, Player_Name text not null, Player_Position text not null, Player_Number text not null, Team text not null);"; 
private static final String DATABASE_NAME = "Score"; 
private static final String DATABASE_TABLE = "Players"; 
private static final int DATABASE_VERSION = 3; 
    public static final String KEY_BODY = "Player_Name"; 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_TITLE = "Player_Position"; 
    public static final String KEY_NUMBER = "Player_Number"; 
    public static final String KEY_TEAM = "Team"; 
private static final String TAG = "PlayerDbAdapter"; 
    private final Context mCtx; 
    private SQLiteDatabase mDb; 
    private DatabaseHelper mDbHelper; 

    public PlayerDbAdapter(Context paramContext) 
    { 
    this.mCtx = paramContext; 
    } 

    public void close() 
    { 
    this.mDbHelper.close(); 
    } 


    public long createPlayers(String playerName, String playerPosition, String playerNumber, String team) 
    { 
    ContentValues localContentValues = new ContentValues(); 
    localContentValues.put(KEY_BODY, playerName); 
    localContentValues.put(KEY_TITLE, playerPosition); 
    localContentValues.put(KEY_NUMBER, playerNumber); 
    localContentValues.put(KEY_TEAM, team); 
    try{ 
     return this.mDb.insert("Players", null, localContentValues); 
    } catch (Exception e) { 
     Log.e("Juma", e.getMessage()); 
    } 

    return 0; 
    } 

logcat的

04-17 14:41:42.890: I/Database(407): sqlite returned: error code = 1, msg = table Players has no column named Player_Number 
04-17 14:41:43.011: E/Database(407): Error inserting Player_Number=23 Team=Chester 1st Team (Men) Player_Position=Goalkeeper Player_Name=a 
04-17 14:41:43.011: E/Database(407): android.database.sqlite.SQLiteException: table Players has no column named Player_Number: , while compiling: INSERT INTO Players(Player_Number, Team, Player_Position, Player_Name) VALUES(?, ?, ?, ?); 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41) 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149) 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 
04-17 14:41:43.011: E/Database(407): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1422) 
04-17 14:41:43.011: E/Database(407): at playerdatabase.PlayerDbAdapter.createPlayer(PlayerDbAdapter.java:46) 
04-17 14:41:43.011: E/Database(407): at your.dissertation.project.SquadActivity$1.onClick(SquadActivity.java:53) 
04-17 14:41:43.011: E/Database(407): at android.view.View.performClick(View.java:2485) 
04-17 14:41:43.011: E/Database(407): at android.view.View$PerformClick.run(View.java:9080) 
04-17 14:41:43.011: E/Database(407): at android.os.Handler.handleCallback(Handler.java:587) 
04-17 14:41:43.011: E/Database(407): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-17 14:41:43.011: E/Database(407): at android.os.Looper.loop(Looper.java:123) 
04-17 14:41:43.011: E/Database(407): at android.app.ActivityThread.main(ActivityThread.java:3647) 
04-17 14:41:43.011: E/Database(407): at java.lang.reflect.Method.invokeNative(Native Method) 
04-17 14:41:43.011: E/Database(407): at java.lang.reflect.Method.invoke(Method.java:507) 
04-17 14:41:43.011: E/Database(407): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-17 14:41:43.011: E/Database(407): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-17 14:41:43.011: E/Database(407): at dalvik.system.NativeStart.main(Native Method) 
+1

您創建查詢是錯誤的,你倒排表和表名 – njzk2

+0

你有一個語法錯誤,如njzk2指出,使用:'CREATE TABLE玩家......'你也應該用你的靜態變量,比如'DATABASE_TABLE',以幫助防止錯別字。 – Sam

+0

感謝似乎修復它 – b0w3rb0w3r

回答

2

下面是正確的語法和使用不斷查詢......

private static final String DATABASE_TABLE = "Players"; 
private static final String DATABASE_CREATE = "CREATE TABLE "+DATABASE_TABLE+" (_id integer primary key autoincrement, Player_Name text not null, Player_Position text not null, Player_Number text not null, Team text not null);"; 
+0

還有更多的常量,不只是'DATABASE_TABLE' ... – Sam

0

這個問題主要是由語法造成的。例如,以前我的代碼是正確的。這是它:

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "(" 
        + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_MESSAGEADDRESS 
        + " TEXT," + COLUMN_MESSAGEBODY + " TEXT " + ")"; 

上述代碼運行正確,但我添加了另一列,它開始導致提到的錯誤。下面是錯誤代碼:

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "(" 
        + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_MESSAGEADDRESS 
        + " TEXT," + COLUMN_MESSAGEBODY + " TEXT" + COLUMN_MESSAGETIME + " LONG" + ")"; 

,你可以看到,當我添加新的列我忘了包括類型TEXT後逗號。這意味着在執行該代碼時,會出現語法錯誤,因爲編譯器將讀取的最後一部分爲:

COLUMN_MESSAGEBODY TEXTCOLUMN_MESSAGETIME LONG 

,而不是:

COLUMN_MESSAGEBODY TEXT, COLUMN_MESSAGETIME LONG 

解決方案:確保你的語法是正確的你注意到空格和逗號。

kindly check the below link for more info: 
http://stackoverflow.com/questions/15123966/android-table-has-no-column-named-variable-name-mysql-database-error/23776738#23776738 
2
1. I think Always Double check your create table command 
    .execSQL("create table " + DATABASE_TABLE+ 
       "(" 
       + KEY_ID + " integer primary key autoincrement," 
       + KEY_NAME + " text not null," 
       + KEY_START + " text not null," 
       + Key_END + " text not null," 
       + KEY_PAGE + " text not null);" 
      ); 

2. if you add or remove any column or change schema then make sure you Upgrade your database version . 
    using previous version through similar exception 
+0

我得到了類似的異常,當我試圖在數據庫列中添加數據(列名稱是錯誤的)。 糾正我,如果我錯了? –

相關問題