2015-12-21 67 views
-1

我正試圖學習如何使用android工作室..我目前正在創建一個非常簡單的應用程序,只是允許用戶輸入和跟蹤用戶和分數。我無法建立此,由於此錯誤消息:找不到SQLite _id列

12-21 16:04:07.037 11681-11681/com.example.cherryjp.scoresexample E/SQLiteLog: (1) no such column: _id 

我想不通這是爲什麼。任何人都可以幫忙嗎?這裏是我的SQLite的適配器代碼:

package com.example.cherryjp.scoresexample; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import java.lang.Override; 
import java.lang.String; 
import java.lang.StringBuilder; 

public class ScoreDataAdapter { 
    // Becomes the filename of the database 
    private static final String DATABASE_NAME = "scores.db"; 
    // Only one table in this database 
    private static final String TABLE_NAME = "scores"; 
    // We increment this every time we change the database schema which will 
    // kick off an automatic upgrade 
    private static final int DATABASE_VERSION = 1; 
    // TODO: Implement a SQLite database 

    public static final String KEY_ID = "_id"; 
    public static final String KEY_NAME = "_name"; 
    public static final String KEY_SCORE = "_score"; 
    private static final String DROP_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME; 
    private static final String CREATE_STATEMENT; 
    static { 
     StringBuilder sb = new StringBuilder(); 
     sb.append("CREATE TABLE " + TABLE_NAME + " ("); 
     sb.append(KEY_ID + " integer primary key autoincrement, "); 
     sb.append(KEY_NAME + " text, "); 
     sb.append(KEY_SCORE + " integer"); 
     sb.append(")"); 
     CREATE_STATEMENT = sb.toString(); 
    } 

    private SQLiteOpenHelper mOpenHelper; 
    private SQLiteDatabase mDatabase; 

    public ScoreDataAdapter(Context context) { 
     //Create SQLiteOpenHelper 
     mOpenHelper = new ScoreDbHelper(context); 
    } 

    public void open() { 
     //open database 
     mDatabase = mOpenHelper.getWritableDatabase(); 

    } 

    public void close() { 
     //close db 
     mDatabase.close(); 
    } 

    public long addScore(Score score) { 
     ContentValues row = getContentValuesfromScore(score); 
     long newId = mDatabase.insert(TABLE_NAME, null, row); 
     score.setId(newId); 
     return 1L; 
    } 

    private ContentValues getContentValuesfromScore(Score score) { 
     ContentValues row = new ContentValues(); 
     row.put(KEY_NAME, score.getName()); 
     row.put(KEY_SCORE, score.getScore()); 
     return null; 
    } 


    public Cursor getScoresCursor() { 
     String[] projection = new String[] {KEY_ID, KEY_NAME, KEY_SCORE }; 
     return mDatabase.query(TABLE_NAME, projection, null, null, null, null, KEY_SCORE + "DESC"); 

    } 


    private static class ScoreDbHelper extends SQLiteOpenHelper { 

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

     @Override 
     public void onCreate(SQLiteDatabase db) { 

      db.execSQL(CREATE_STATEMENT); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.d(ScoresListActivity.SLS, "Upgrading the database from revision " + oldVersion + " to " + newVersion); 
      db.execSQL(DROP_STATEMENT); 
      onCreate(db); 
     } 
    } 
} 

由於提前, 約翰

回答

0

它看起來像你缺少你的桌子上創建語句空間

sb.append(KEY_NAME + "text, "); // will become "_nametext, " 
sb.append(KEY_SCORE + "integer"); // will become "_scoreinteger" 

應該

sb.append(KEY_NAME + " text, "); 
sb.append(KEY_SCORE + " integer"); 
+0

這並沒有改變什麼:

來修復只需添加鍵名和密鑰類型之間的空間?同樣的錯誤信息 –

0

您的應用在創建數據庫時會崩潰 - 因爲您的表創建代碼將產生無效的SQL語句。試圖建立...其他想法,當

- sb.append(KEY_NAME + "text, "); 
+ sb.append(KEY_NAME + " text, "); 
- sb.append(KEY_SCORE + "integer"); 
+ sb.append(KEY_SCORE + " integer"); 
+0

這並沒有改變任何東西,當試圖建立...其他想法?同樣的錯誤信息 –

+0

您應該在生成之前更改 - 添加空格。在執行CREATE_TABLE語句之前添加'Log.d()'調用 - 以查看正在發生的特定事件。 –