Java Android sqlite游标查询
我正在学习教程。我现在正在尝试更改系统,这样不仅有字段“comment”,还有字段“item”和“price” 现在我有两个冒号,而不是一个,光标似乎是导致应用程序崩溃! 例如,下面是代码:Java Android sqlite游标查询,java,android,Java,Android,我正在学习教程。我现在正在尝试更改系统,这样不仅有字段“comment”,还有字段“item”和“price” 现在我有两个冒号,而不是一个,光标似乎是导致应用程序崩溃! 例如,下面是代码: public List<Item> getAllItems() { List<Item> items = new ArrayList<Item>(); Cursor cursor = database.que
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;
}
是什么导致了这个错误,我相信这与我有一个额外的专栏有关。有人能告诉我如何编辑这一行让它工作,我已经尝试了,但不明白这个光标查询
编辑:通过将所有列替换为空来运行。但现在它崩溃了:
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);
}
在线:
Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
以下是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);
}
}
编辑:问题是数据库最初创建时没有一个字段。IMHO,
allColumns
与MySQLiteHelper.TABLE\u ITEMS
定义不一致。在这里输入所有分词对象的声明和当前值
还有一种可能性-
数据库
对象未正确初始化,目前为空。您应该检查这种情况。IMHO,allColumns
与MySQLiteHelper.TABLE\u ITEMS
定义不一致。在这里输入所有分词对象的声明和当前值
还有一种可能性-
数据库
对象未正确初始化,目前为空。您应该检查此类情况。请添加logcat错误StackTrace您忘记在此处放置“数据库”和“MySQLiteHelper.TABLE_项”的声明,请确保在运行带有2的代码之前删除了带有1列的应用程序,因为数据库只会更新而不会替换。您可以从emulator获取DB文件,并使用sqlite.exe确保使用所有列正确创建该文件。您可能在allColumns
中有错误的列。请尝试将查询中的所有列替换为null。请添加logcat错误堆栈跟踪您忘记将“数据库”和“MySQLiteHelper.TABLE_项目“在使用2运行代码之前,请确保删除了包含1列的应用程序,因为数据库只会更新而不会替换。您可以从emulator获取DB文件,并使用sqlite.exe确保使用所有列正确创建该文件。您可能在allColumns
中有错误的列。请尝试在查询中用null替换allColumns
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);
}
}