2011-12-12 124 views
0

我真的很抱歉問這個愚蠢的問題。過去1小時,我一直在探究這個錯誤,無法從我眼中發現問題。如果有人能幫我找到問題,我會很感激。我知道這不是一個錯誤處理博客,但我沒有選擇。以下是我的DB代碼:語法錯誤使用sqlite

private static final String DATABASE_NAME = "RCdata"; 
private static final String DATABASE_TABLE = "events"; 
private static final int DATABASE_VERSION = 1; 
private static final String TAG = "RCDataBaseAdapter"; 
private static final String KEY_ROWID = "_id"; 
private static final String KEY_EVENT = "eventsname"; 
private static final String KEY_DESCRIPTION ="description"; 

// Database creation sql statement 
private static final String DATABASE_CREATE_EVENTS = 
     "create table events (_id integer primary key autoincrement, " 
     + "eventsname text not null, " 
     + "description text not null);"; 


private Context context; 
private DatabaseHelper DBHelper; 
private SQLiteDatabase db; 
     public RCDatabaseAdapter(Context ctx) 
{ 
    //super(ctx, DATABASE_NAME, null, DATABASE_VERSION); 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
    // TODO Auto-generated constructor stub 
} 

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 
    db.execSQL(DATABASE_CREATE_EVENTS); 
    //db.execSQL(DATABASE_CREATE_Priests); 


} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 
    Log.w(TAG, "Upgrading database from version " + oldVersion 
      + " to " 
      + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS Events"); 
     onCreate(db); 

} 
} 
//---opens the database--- 
public RCDatabaseAdapter open() throws SQLException 
{ 
    db = DBHelper.getWritableDatabase(); 
    return this; 
} 

//---closes the database---  
public void close() 
{ 
    DBHelper.close(); 
} 

public long insertEvent(String eventname, String description) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_EVENT, eventname); 
    initialValues.put(KEY_DESCRIPTION, description); 

    return db.insert(DATABASE_TABLE, null, initialValues); 

} 


//---deletes a particular event--- 
public boolean deleteEvent(long rowId) 
{ 
    return db.delete(DATABASE_CREATE_EVENTS, KEY_ROWID + 
      "=" + rowId, null) > 0; 
} 


public Cursor getAllTitles() 
{ 
    return db.query(DATABASE_CREATE_EVENTS, new String[] { 
      KEY_ROWID, 
      KEY_EVENT, 
      KEY_DESCRIPTION}, 
      null, 
      null, 
      null, 
      null, 
      null); 
} 

//---retrieves a particular title--- 
public Cursor getEvent(long rowId) throws SQLException 
{ 

    Cursor mCursor = 
      db.query(true, DATABASE_CREATE_EVENTS, new String[] { 
        KEY_ROWID, 
        KEY_EVENT, 
        KEY_DESCRIPTION      
        }, 
        KEY_ROWID + "=" + rowId, 
        null, 
        null, 
        null, 
        null, 
        null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor;     


} 

我收到以下錯誤:

near 「create」: syntax error: , 
while compiling: SELECT _id, 
eventsname, description 
FROM create table events (_id 
integer primary key 
autoincrement, eventsname 
text not null, description text 
not null); 
+1

您可以在使用DATABASE_CREATE_Events的地方添加代碼嗎?好像你正在嘗試從表中「選擇」數據,同時傳遞一個'create'查詢。 – abhinav

+0

我編輯了上面的代碼。感謝您的發起 – user1031746

+0

DATABASE_CREATE_EVENTS我已將名稱更改爲全部大寫,以解決可能存在的問題,但仍然收到相同的錯誤。 – user1031746

回答

1

您的getAllTitles方法,deleteEventgetEvent都使用create語法查詢來獲取記錄。您需要將適當的語法傳遞給query方法。文檔here 例如,您的getEvent方法可能會像這樣工作。

db.query(true, "events", new String[] { 
        KEY_ROWID, 
        KEY_EVENT, 
        KEY_DESCRIPTION      
        }, 
        KEY_ROWID + "=" + rowId, 
        null, 
        null, 
        null, 
        null, 
        null); 
+0

非常感謝你。我從http://www.devx.com/wireless/Article/40842/1763?supportItem=4獲取了這段代碼,我是android新手,並嘗試首次連接到數據庫。我想知道爲什麼會出現這種差異,我相信我很難弄清楚。有效。有什麼我可以通過例子學習的數據庫網站嗎?例如,如果我有數據庫的多個表,我是否需要爲每個表創建所有的get,insert,update方法? – user1031746