0

我的应用程序无法创建简单表。请看下面的代码,

数据库处理程序.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 shell 检查了在数据库中创建的表,没有名称为 PASSENGER_INFO 的表。请帮忙 !

4

4 回答 4

0

您最后在创建表中缺少一个分号

+ " TEXT" + ");";
于 2013-01-23T13:55:00.723 回答
0

“);”后面的分号 缺少查询的和。Ps 不鼓励您将数据插入数据库的方式,您应该使用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);"
        );
于 2013-01-23T14:06:57.580 回答
0

您是否尝试过增加数据库编号?另外,尝试更改此行

+ TABLE_COLUMN_SEAT_NO
    + " TEXT PRIMARY KEY, "

+ TABLE_COLUMN_SEAT_NO
    + " TEXT, "

添加一个单独的列来表示 rowId,更新您的数据库版本号并重新运行。

于 2013-01-23T17:58:03.637 回答
0

假设您创建了表 ( PASSENGER_INFO),但没有SEAT_NO稍后添加它的列。您需要做的是增加DB_VERSION并重新运行应用程序。什么会导致 DatabaseHandleronUpdate()运行。您没有显示该代码,但希望它删除表,然后调用onCreate()以重新创建表(现在使用SEAT_NO列)。

旁注:我会将该表的主键更改_id为您拥有它的方式,您将只能存储一个航班的座位价值......当然设计取决于您。

于 2013-01-23T17:52:31.150 回答