[英]Android sqlite cursor query
I'm following the http://www.vogella.de/articles/AndroidSQLite/article.html tutorial. 我正在关注http://www.vogella.de/articles/AndroidSQLite/article.html教程。 I am now trying to change the system so there is not just field 'comment' but rather field 'item' and 'price'.
我现在正在尝试更改系统,因此不仅存在字段“ comment”,而且存在字段“ item”和“ price”。
Now that I have 2 Coloums rather then one the cursor seems to be causing the app to crash! 现在,我有2个Coloums而不是一个,光标似乎正在导致应用程序崩溃! For example here is the code below:
例如,下面是下面的代码:
public List<Item> getAllItems() {
List<Item> items = new ArrayList<Item>();
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;
}
LINE : LINE:
Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
allColumns, null, null, null, null, null);
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.
有人可以告诉我如何编辑此行以使其正常工作吗,我已经尝试过但不理解此游标查询。
EDIT: By replacing allcolumns with null it ran. 编辑:通过将所有列替换为null来运行。 But now its crashing on:
但是现在它崩溃了:
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);
}
on line: 在线:
Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
Here is the MySQLHelper.js: 这是MySQLHelper.js:
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);
}
}
Edit: The issue was the database was created initially without one of the fields. 编辑:问题是最初创建数据库时没有其中一个字段。
IMHO, allColumns
doesn't sit with the MySQLiteHelper.TABLE_ITEMS
definition. 恕我直言,
allColumns
不包含MySQLiteHelper.TABLE_ITEMS
定义。 Put here declarations and current values of all participying objects. 将所有参与对象的声明和当前值放在此处。
One more possibility - database
object is not initialized properly and is null at the moment. 另一种可能性-
database
对象未正确初始化,目前为空。 You should check for such situations. 您应该检查这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.