mardi 3 février 2015

Android SQLite not inserting in the order I tell it to

I have an object that that is called to add a row to the database but for reasons I can't figure out, it is attempting to insert it not in the order I tell it to.


Here is the code for the method



public Coin createCoin(String create_year, String create_specialty, String create_mintage, String create_have, int create_mint) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.MINT, create_mint);
values.put(MySQLiteHelper.YEAR, create_year);
values.put(MySQLiteHelper.MINTAGE, create_mintage);
values.put(MySQLiteHelper.HAVE, create_have);
values.put(MySQLiteHelper.SPECIALTY, create_specialty);
//(String table, String nullColumnHack, ContentValues values)
long insertId = database.insert(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE, null,
values);
//(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Cursor cursor = database.query(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE,
allColumns, MySQLiteHelper.YEAR, null,
null, null, null);
cursor.moveToFirst();
Coin newCoin = cursorToCoin(cursor);
cursor.close();
return newCoin;
}
public ArrayList<Coin> getAllCoins() {
ArrayList<Coin> coins = new ArrayList<Coin>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Coin coin = cursorToCoin(cursor);
coins.add(coin);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return coins;
}
private Coin cursorToCoin(Cursor cursor) {
Coin coin = new Coin();
coin.setYear(cursor.getString(0));
coin.setSpecialty(cursor.getString(1));
coin.setMintage(cursor.getString(2));
return coin;
}


Now I would think it would attempt to insert the data as



INSERT INTO penny_flying_eagle(mint,year,mintage,have,specialty) VALUES (?,?,?,?,?)


but instead it is giving me an error



66-1166/com.example.dommol.testlist E/SQLiteLog﹕ (1) table penny_flying_eagle has no column named mint
02-03 21:31:42.097 1166-1166/com.example.dimmel.testlist E/SQLiteDatabase﹕ Error inserting mint=0 year=1858 mintage=24,600,000 specialty=Not NULL have=D
android.database.sqlite.SQLiteException: table penny_flying_eagle has no column named mint (code 1): , while compiling: INSERT INTO penny_flying_eagle(mint,year,mintage,specialty,have) VALUES (?,?,?,?,?)


My thought is it is telling me there is no column named mint because it is attempting to put mint in the place of year and not finding the column there. Why is this trying to insert the values in a different order than the one I specified?


Aucun commentaire:

Enregistrer un commentaire