1

我正在尝试将列表中的一些值添加到 android sqlite 数据库,但不知何故,它似​​乎找不到表,而且我尝试在表中添加的东西根据 logcat 的顺序也不正确。我又使用了 3 个表,并在没有问题的表中添加了值。我已经为此苦苦挣扎了一段时间,但我似乎看不到问题所在。有任何想法吗?这是我的代码和错误消息:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    currentCurrency = new ArrayList<String>();
    currentCurrencyDetails = new ArrayList<String>();
    currentCurrency.add("€");
    currentCurrencyDetails.add("€ EURO");
    currentCurrency.add("£");
    currentCurrencyDetails.add("£ UK POUND");
    currentCurrency.add("$");
    currentCurrencyDetails.add("$ USA DOLLAR");
    currentCurrency.add("¥");
    currentCurrencyDetails.add("¥ JAPAN YEN");

    db = new DBAdapter(this);
    db.open();
    for (int i = 0; i <= currentCurrency.size() - 1; i++) {
        if (i == 0) {
            db.insertDefaultCurrency(currentCurrency.get(i).toString(),
                    currentCurrencyDetails.get(i).toString(), 1);
        } else {
            db.insertDefaultCurrency(currentCurrency.get(i).toString(),
                    currentCurrencyDetails.get(i).toString(), 0);
        }
    }

}

DBAdapter 类:

public static final String MY_CURRENT_CURRENCY_TABLE = "MyCurrency";
public static final String MY_CURRENT_CURRENCY = "MyCurrentCurrency";
public static final String MY_CURRENT_CURRENCY_NAMES = "MyCurrentCurrencyNames";
public static final String MY_CURRENT_CURRENCY_BOOLEAN = "MyCurrentCurrencyBoolean";

private static final String DATABASE4_CREATE = "CREATE TABLE "
        + MY_CURRENT_CURRENCY_TABLE + "(" + MY_CURRENT_CURRENCY + " TEXT NOT NULL, "
        + MY_CURRENT_CURRENCY_NAMES + " TEXT NOT NULL, "
        + MY_CURRENT_CURRENCY_BOOLEAN + "INTEGER NOT NULL);";

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        db.execSQL(DATABASE1_CREATE);
        db.execSQL(DATABASE2_CREATE);
        db.execSQL(DATABASE3_CREATE);
        db.execSQL(DATABASE4_CREATE);
    }

public long insertDefaultCurrency(String currency, String currencyDetails, int usedBoolean) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(MY_CURRENT_CURRENCY, currency);
    initialValues.put(MY_CURRENT_CURRENCY_NAMES, currencyDetails);
    initialValues.put(MY_CURRENT_CURRENCY_BOOLEAN, usedBoolean);
    return db.insert(MY_CURRENT_CURRENCY_TABLE, null, initialValues);
}

日志信息:

01-28 10:31:26.865: E/SQLiteLog(8623): (1) table MyCurrency has no column named MyCurrentCurrencyBoolean
01-28 10:31:26.880: E/SQLiteDatabase(8623): Error inserting MyCurrentCurrencyBoolean=1 MyCurrentCurrency=€ MyCurrentCurrencyNames=€ EURO
01-28 10:31:26.880: E/SQLiteDatabase(8623): android.database.sqlite.SQLiteException: table MyCurrency has no column named MyCurrentCurrencyBoolean (code 1): , while compiling: INSERT INTO MyCurrency(MyCurrentCurrencyBoolean,MyCurrentCurrency,MyCurrentCurrencyNames) VALUES (?,?,?)
4

1 回答 1

8

改变:

+ MY_CURRENT_CURRENCY_BOOLEAN + "INTEGER NOT NULL);";

+ MY_CURRENT_CURRENCY_BOOLEAN + " INTEGER NOT NULL);";

"和之间的缺失空间INTEGER几乎肯定是您的错误的原因。之后,清除应用程序的数据并再次运行,以便重新创建数据库。

于 2013-01-28T08:51:32.437 回答