2012-10-09 92 views
0

下面是我使用的代碼..以及它顯示的錯誤..我已經經歷了每一行代碼,但我無法弄清楚..請幫助我..android-創建數據庫的錯誤

public class CenterCollectionDatabase extends Activity { 

public static final String KEY_ID = "id"; 
public static final String KEY_ROUNDID = "round"; 
public static final String KEY_PLAYERID = "playerid"; 
public static final String KEY_MAAL = "maal"; 
public static final String KEY_POINT = "point"; 
public static final String KEY_WON = "won"; 
private static final String DATABASE_NAME ="center_collection"; 
private static final String DATABASE_TABLE= "transaction"; 
private static final int DATABASE_VERSION = 1; 
private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

private static class DbHelper extends SQLiteOpenHelper{ 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 

     db.execSQL("CREATE TABLE " + DATABASE_TABLE +" ("+ 
       KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ 
       KEY_ROUNDID + " INTEGER, "+ 
       KEY_PLAYERID + " INTEGER, "+ 
       KEY_POINT + " INTEGER, "+ 
       KEY_MAAL + " INTEGER, "+ 
       KEY_WON+ " INTEGER);"   
      ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 

} 

public CenterCollectionDatabase(Context c) 
{ 
    ourContext = c; 
} 

public CenterCollectionDatabase open() throws SQLException{ 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 

} 
public void close() 
{ 
    ourHelper.close(); 
} 

public long add_score(int round, int playerid, int point, int maal, int won) { 

    ContentValues cv = new ContentValues(); 
    cv.put(KEY_ROUNDID, round); 
    cv.put(KEY_PLAYERID, playerid); 
    cv.put(KEY_POINT, point); 
    cv.put(KEY_MAAL, maal); 
    cv.put(KEY_WON, won); 

    return ourDatabase.insert(DATABASE_TABLE, null, cv); 
} 
    } 
下面

是用於將數據保存在數據庫

if(counter>0) 
      { 
       Toast.makeText(getApplicationContext(), "Oops Mike! please fill all fields", Toast.LENGTH_SHORT).show(); 
      }else{ 

       CenterCollectionDatabase db = new CenterCollectionDatabase(ScoreEntry.this); 
       db.open(); 

       for(int i=0; i<number_of_player; i++) 
       { 
        int round = 1; 
        int playerid = i; 
        int point = Integer.parseInt(Spoint[i]); 
        int maal = Integer.parseInt(Smaal[i]); 
        int won_who = 0; 

        db.add_score(round, playerid, point, maal, won_who); 

       } 
       db.close(); 

       Toast.makeText(getApplicationContext(), "Record Sucessfully Saved", Toast.LENGTH_SHORT).show(); 

       Intent intent = new Intent(ScoreEntry.this, ScoreBoard.class); 
       startActivity(intent); 

      } 

下面的代碼是錯誤

10-09 23:25:52.342: E/AndroidRuntime(584): FATAL EXCEPTION: main 
10-09 23:25:52.342: E/AndroidRuntime(584): android.database.sqlite.SQLiteException: near "transaction": syntax error: CREATE TABLE transaction (id INTEGER PRIMARY KEY AUTOINCREMENT, round INTEGER, playerid INTEGER, point INTEGER, maal INTEGER, won INTEGER); 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727) 
10-09 23:25:52.342: E/AndroidRuntime(584): at com.vivek.marriage_center_collection.CenterCollectionDatabase$DbHelper.onCreate(CenterCollectionDatabase.java:41) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106) 
10-09 23:25:52.342: E/AndroidRuntime(584): at com.vivek.marriage_center_collection.CenterCollectionDatabase.open(CenterCollectionDatabase.java:68) 
10-09 23:25:52.342: E/AndroidRuntime(584): at com.vivek.marriage_center_collection.ScoreEntry$1.onClick(ScoreEntry.java:133) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.view.View.performClick(View.java:2408) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.view.View$PerformClick.run(View.java:8816) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.os.Handler.handleCallback(Handler.java:587) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.os.Looper.loop(Looper.java:123) 
10-09 23:25:52.342: E/AndroidRuntime(584): at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-09 23:25:52.342: E/AndroidRuntime(584): at java.lang.reflect.Method.invokeNative(Native Method) 
10-09 23:25:52.342: E/AndroidRuntime(584): at java.lang.reflect.Method.invoke(Method.java:521) 
10-09 23:25:52.342: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-09 23:25:52.342: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-09 23:25:52.342: E/AndroidRuntime(584): at dalvik.system.NativeStart.main(Native Method) 
+0

請提供它正在線about.l –

+0

錯誤在這裏'近「事務」:語法錯誤:CREATE TABLE事務(INTEGER PRIMARY KEY AUTOINCREMENT,round INTEGER,playerid INTEGER,點INTEGER,maal INTEGER,贏INTEGER);'在那一行 –

回答

2

我認爲問題是你的表的名字。這是sqlite中的一個關鍵字,所以你不能用它作爲表名。

從鏈接:「...... SQL標準可能不能作爲表名的關鍵字數量龐大......」

SQLite docs

+0

awesome dude ..它的工作..謝謝 – user98239820

0

如果沒有把握這是它尚未

public static final String KEY_ID = "id"; 

應該

public static final String KEY_ID = "_id"; 
在sqlite的世界