Dommol
Dommol

Reputation: 191

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_mint, int create_have) {
    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?

Edit: code for create table

public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_PENNY_FLYING_EAGLE = "penny_flying_eagle";
public static final String YEAR = "year";
public static final String SPECIALTY = "specialty";
public static final String MINTAGE = "mintage";
public static final String HAVE = "have";
public static final String MINT = "mint";

private static final String DATABASE_NAME = "coins.db";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_PENNY_FLYING_EAGLE + "("
        + MINT + " text, "
        + YEAR + " text, "
        + MINTAGE + " text not null, "
        + SPECIALTY + " text, "
        + HAVE + " integer, "
        + "primary key (" + YEAR + ", " + SPECIALTY + ", " + MINTAGE + ") on conflict ignore );";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);

Upvotes: 1

Views: 451

Answers (1)

W I Z A R D
W I Z A R D

Reputation: 1224

This issue will cause due to two things :

  1. If you renamed the column name. (that is mint column) because once the database is created it will not alter the table when changed the column name.

  2. If you added new column name for the already existing table.(that is mint column)

Solution:

  1. Change(Upgrade) the DATABASE_VERSION from 1 to 2 or so on.

  2. Delete the database from data/data/"your app name"/databases/"your database" and run the application so that the new database will be created.

Upvotes: 3

Related Questions