Reputation: 423
Am trying to add some values from a list to the android sqlite database but somehow it doesnt seem to find the table, and also the things i try to add in the table are not in the correct order according to logcat. I used 3 more tables and i added values into those with no problem. Ive been struggling on this for a while but i cant seem to see the problem. Any ideas? Here is my code and error message:
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 class:
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 message:
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 (?,?,?)
Upvotes: 1
Views: 2687
Reputation: 82543
Change:
+ MY_CURRENT_CURRENCY_BOOLEAN + "INTEGER NOT NULL);";
to
+ MY_CURRENT_CURRENCY_BOOLEAN + " INTEGER NOT NULL);";
The missing space between "
and INTEGER
is almost definitely the cause of your error. After that, clear your app's data and run it again so that the database is recreated.
Upvotes: 8