我试图从列表中添加一些值到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);
}
logcat消息:
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 (?,?,?)
答案 0 :(得分:8)
变化:
+ MY_CURRENT_CURRENCY_BOOLEAN + "INTEGER NOT NULL);";
到
+ MY_CURRENT_CURRENCY_BOOLEAN + " INTEGER NOT NULL);";
"
和INTEGER
之间缺少的空格几乎肯定是导致错误的原因。之后,清除应用程序的数据并再次运行,以便重新创建数据库。