Android sqlite cursor query
<p>Hey all I'm new to android and sqlite, I'm following the <a href="http://www.vogella.de/articles/AndroidSQLite/article.html" rel="nofollow">http://www.vogella.de/articles/AndroidSQLite/article.html</a> tutorial. I am now trying to change the system so there is not just field 'comment' but rather field 'item' and 'price'.</p>

<p>Now that I have 2 Coloums rather then one the cursor seems to be causing the app to crash!
For example here is the code below:</p>

<pre><code>        public List&lt;Item&gt; getAllItems() {
            List&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
            Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                    allColumns, null, null, null, null, null);
            cursor.moveToFirst();

            while (!cursor.isAfterLast()) {
                Item item = cursorToItem(cursor);
                items.add(item);
                cursor.moveToNext();
            }
            // Make sure to close the cursor
            cursor.close();
            return items;
        }

        private Item cursorToItem(Cursor cursor) {
            Item item = new Item();
            item.setId(cursor.getLong(0));
            item.setItem(cursor.getString(1));
            item.setPrice(cursor.getString(2));
            return item;
        }
</code></pre>

<p>LINE : </p>

<pre><code> Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                    allColumns, null, null, null, null, null);
</code></pre>

<p>Is what is causing the error, I believe it has something to do with the fact that I have an additional column. can someone please tell me how to edit this line to get it to work, I have tried but don't understand this cursor query.</p>

<p>EDIT: By replacing allcolumns with null it ran. But now its crashing on:</p>

<pre><code>public Item createItem(String item, String price) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_ITEM, item);
        values.put(MySQLiteHelper.COLUMN_PRICE, price);
        long insertId = database.insert(MySQLiteHelper.TABLE_ITEMS, null,
                values);
        // To show how to query
        Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        return cursorToItem(cursor);
    }
</code></pre>

<p>on line:    </p>

<pre><code>Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
</code></pre>

<p>Here is the MySQLHelper.js:</p>

<pre><code>package nupos.nupay.app;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_ITEMS = "items";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_ITEM = "item";
public static final String COLUMN_PRICE = "price";

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

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_ITEMS + "( " + COLUMN_ID
        + " integer primary key autoincrement, " + COLUMN_ITEM
        + " text not null," + COLUMN_PRICE
        +"  text not null);";

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

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);


    onCreate(db);
}

 }
</code></pre>

<p>Edit: The issue was the database was created initially without one of the fields </p>
