2013-01-23 81 views
0

我的應用程序無法創建簡單的表格。請看下面的代碼,無法在android中創建表格

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper { 

private static final int DB_VERSION = 1; 
private static final String DB_NAME = "PASSENGER_INFO_DB"; 
private static final String TABLE_NAME = "PASSENGER_INFO"; 
private static final String TABLE_COLUMN_SEAT_NO = "SEAT_NO"; 
private static final String TABLE_COLUMN_NAME = "NAME"; 
private static final String TABLE_COLUMN_AGE = "AGE"; 
private static final String TABLE_COLUMN_GENDER = "GENDER"; 
private static final String TABLE_COLUMN_FOOD_PREF = "FOOD_PREF"; 

private static final String CREATE_PASSENGER_INFO_TABLE = "CREATE TABLE IF NOT EXISTS " 
     + TABLE_NAME + " (" + TABLE_COLUMN_SEAT_NO 
     + " TEXT PRIMARY KEY, " + TABLE_COLUMN_NAME + " TEXT, " 
     + TABLE_COLUMN_AGE + " INTEGER, " + TABLE_COLUMN_GENDER 
     + " INTEGER, " + TABLE_COLUMN_FOOD_PREF + " TEXT" + ")"; 

public DatabaseHandler(Context context) { 
    super(context, DB_NAME, null, DB_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(CREATE_PASSENGER_INFO_TABLE); 
} 
public void addPassengerInfo(PassengerInfo passInfo) { 
    SQLiteDatabase addPass = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 

    values.put(TABLE_COLUMN_SEAT_NO, passInfo.getM_seatNo()); 
    values.put(TABLE_COLUMN_NAME, passInfo.getM_name()); 
    values.put(TABLE_COLUMN_AGE, passInfo.getM_age()); 
    values.put(TABLE_COLUMN_GENDER, passInfo.getM_gender()); 
    values.put(TABLE_COLUMN_FOOD_PREF, passInfo.getM_foodPref()); 

    addPass.insert(TABLE_NAME, null, values); 
    addPass.close(); 
} 
在MainActivity.java

DatabaseHandler dbhandler = new DatabaseHandler(this); 
addSeat = "1A"; 
passName = "NA"; 
passAge = 0; 
passGender = -1; 
passFoodPref = "NA"; 
dbhandler.addPassengerInfo(new PassengerInfo(addSeat, passName, passAge, passGender, passFoodPref)); 

(PassengerInfo.class具有領域getter和setter方法)

當我運行這個我得到以下錯誤logcat,

01-23 19:09:18.311: I/Database(1303): sqlite returned: error code = 1, msg = table PASSENGER_INFO has no column named SEAT_NO 
01-23 19:09:18.370: E/Database(1303): Error inserting FOOD_PREF=NA NAME=NA AGE=0 GENDER=-1 SEAT_NO=1A 

我也檢查過通過adb外殼在數據庫中創建的表,沒有名爲PASSENGER_INFO的表。 請幫忙!

+0

必須有一個名爲PASSENGER_INFO的表,因爲錯誤消息明確指出。你在哪裏尋找數據庫? –

+0

您是否在表格中添加了「_id」列? –

+0

請參閱http://stackoverflow.com/questions/10349337/creating-tables-in-sqlite-database-on-android –

回答

0

你試過增加你的數據庫數量嗎?另外,嘗試改變這一行

+ TABLE_COLUMN_SEAT_NO 
    + " TEXT PRIMARY KEY, " 

+ TABLE_COLUMN_SEAT_NO 
    + " TEXT, " 

添加一個單獨的列,將代表ROWID,更新數據庫的版本號,然後重新運行。

0

你缺少最後在SEMICON創建表

+ " TEXT" + ");"; 
+0

我試過這個,但仍面臨同樣的問題。 –

0

後分號 「);」在和查詢丟失。附註:您正在將數據插入到數據庫中的方式並不鼓勵你應該使用Contentvalues

 ContentValues row = new ContentValues(); 
    row.put("SEAT_NO",tid); 
    row.put("NAME", sid); 
    row.put("AGE", rsubject); 
    row.put("GENDER",rdescr); 
    row.put("FOOD_PREF", day); 


    long chk = db.insert("PASSENGER_INFO",null, row); 

而且適當的查詢會像

db.execSQL(" CREATE TABLE " + DATABASE_MARKSTABLE + " (" + 
       "SEAT_NO" + " TEXT PRIMARY KEY, " + 
       "NAME" + " TEXT NOT NULL, " + 
       "AGE" + " INTEGER NOT NULL, " + 
       "GENDER" + " INTEGER NOT NULL, " + 
       "FOOD_PREF" + " TEXT);" 
     ); 
+0

「之後我用分號嘗試過);」但沒用。謝謝 ! –

0

假設您創建的表( PASSENGER_INFO)沒有SEAT_NO列在稍後添加它。你需要做的是增加DB_VERSION並重新運行應用程序。什麼會導致DatabaseHandler的onUpdate()運行。您沒有顯示該代碼,但希望它刪除表格,然後調用onCreate()重新創建表格(現在使用SEAT_NO列)。

邊注:我會改變表的主鍵到_id作爲你的方式,你將只能存儲一個航班的價值座位......當然,設計是由你決定的。