所以我一直在尝试在我的sqlite数据库中查询我的android应用程序。但是,似乎我的光标无法正常工作,因为我可以获得任何结果。
我知道我的数据库中只有3个项目,但是我在查询中输入的内容是3个项目的有效属性。但是仍然没有结果。
下面是我用于查询功能和数据库模型的代码。
// To get data from DB by querying the items selected
public String getData(int firstSelection, int secondSelection, int thirdSelection,
int fourthSelection, int fifthSelection)
{
SQLiteDatabase db = dbHelper.getWritableDatabase();
String firstSelectionStr, secondSelectionStr, thirdSelectionStr, fourthSelectionStr, fifthSelectionStr;
firstSelectionStr = Integer.toString(firstSelection);
secondSelectionStr = Integer.toString(secondSelection);
thirdSelectionStr = Integer.toString(thirdSelection);
fourthSelectionStr = Integer.toString(fourthSelection);
fifthSelectionStr = Integer.toString(fifthSelection);
//String[] columns = {DBHelper.UID,DBHelper.CNAME};
//Cursor cursor = db.query(DBHelper.TABLE_NAME,columns,null,null,null,null,null);
String selectQuery = "SELECT * FROM "+ DBHelper.TABLE_NAME + " WHERE " + DBHelper.FIRST_ATTRIBUTE + "=? "
+ " AND " + DBHelper.SECOND_ATTRIBUTE + "=? " + " AND " + DBHelper.THIRD_ATTRIBUTE + "=? " + " AND " + DBHelper.FOURTH_ATTRIBUTE + "=? "
+ " AND " + DBHelper.FIFTH_ATTRIBUTE + "=?";
Cursor cursor=db.rawQuery(selectQuery, new String[] {firstSelectionStr, secondSelectionStr, thirdSelectionStr,
fourthSelectionStr, fifthSelectionStr});
StringBuilder buffer = new StringBuilder();
cursor.moveToFirst();
if (cursor != null) {
int tresult = cursor.getCount();
// Append every data together
do {
//int cursorID = cursor.getInt(cursor.getColumnIndex(DBHelper.UID));
String chosenItem = cursor.getString(cursor.getColumnIndex(DBHelper.CNAME));
buffer.append(chosenItem + " ");
} while (cursor.moveToNext());
/*while (cursor.moveToNext())
{
//int cursorID = cursor.getInt(cursor.getColumnIndex(DBHelper.UID));
String chosenItem = cursor.getString(cursor.getColumnIndex(DBHelper.CNAME));
buffer.append(chosenItem + " ");
}*/
}
return buffer.toString();
}
static class DBHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "CraftsAppDatabase.db"; // Database Name
private static final String TABLE_NAME = "CraftTools"; // Table Name
private static final String RESULT_TABLE = "Result"; // Table Name
private static final int DATABASE_Version = 1; // Database Version
private static final String UID="_id"; // Column I (Primary Key)
private static final String CNAME = "Craft_Name"; //Column II
private static final String RESULT = "Result_Name"; //Column II
private static final String FIRST_ATTRIBUTE = "First_Attribute"; //Column III
private static final String SECOND_ATTRIBUTE = "Second_Attribute"; //Column IV
private static final String THIRD_ATTRIBUTE = "Third_Attribute"; //Column V
private static final String FOURTH_ATTRIBUTE = "Fourth_Attribute"; //Column VI
private static final String FIFTH_ATTRIBUTE = "Fifth_Attribute"; //Column VII
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+CNAME+" VARCHAR(255)" +
", "+FIRST_ATTRIBUTE+" VARCHAR(255), "+SECOND_ATTRIBUTE+" VARCHAR(255)" +
", "+THIRD_ATTRIBUTE+" VARCHAR(255), "+FOURTH_ATTRIBUTE+" VARCHAR(255)" +
", "+FIFTH_ATTRIBUTE+" VARCHAR(255));";
private static final String CREATE_OTHER_TABLE = "CREATE TABLE "+RESULT_TABLE+
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+RESULT+" VARCHAR(255));";
private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+RESULT_TABLE;
private Context context;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context=context;
}
/*public void deleteTable(SQLiteDatabase db) {
db = getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS CraftTools");
}*/
public void onCreate(SQLiteDatabase db) {
db.execSQL(DROP_TABLE);
db.execSQL(CREATE_OTHER_TABLE);
db.execSQL(CREATE_TABLE);
db.execSQL("INSERT INTO " + TABLE_NAME + "(Craft_Name, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
"VALUES ('Landscape Drawing', '1', '4','8', '0', '0')");
db.execSQL("INSERT INTO " + TABLE_NAME + "(Craft_Name, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
"VALUES ('Popsicle Sticks House', '2', '3','0', '0', '0')");
db.execSQL("INSERT INTO " + TABLE_NAME + "(Craft_Name, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
"VALUES ('Sunset Painting', '4', '7','10', '0', '0')");
/*try {
db.execSQL(CREATE_TABLE);
} catch (Exception e) {
Message.message(context,""+e);
}*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//db.execSQL(DROP_TABLE);
onCreate(db);
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE CraftTools ADD COLUMN FIRST_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN SECOND_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN THIRD_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN FOURTH_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN FIFTH_ATTRIBUTE INTEGER DEFAULT 0");
//onCreate(db);
/*try {
Message.message(context,"OnUpgrade");
db.execSQL(DROP_TABLE);
onCreate(db);
}catch (Exception e) {
Message.message(context,""+e);*/
}
}
}
我的错误日志记录在此行下方。我不确定为什么我的结果为空,或者为什么什么都没有放入缓冲区。
11-27 22:43:28.898 4502-4502/com.example.android.androidcraftsappprototype E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.androidcraftsappprototype, PID: 4502
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.android.androidcraftsappprototype.DBAdapter.getData(DBAdapter.java:86)
at com.example.android.androidcraftsappprototype.SetupPage$2.onClick(SetupPage.java:118)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:2)
我认为您的课程会根据查询返回0个项目。因此您应在以下条件下编写
cursor.moveToFirst(); <--- instead of this
if(cursor.moveToFirst()){
// here all your code
}
答案 1 :(得分:-1)
尝试此代码。
public class DBHelper extends SQLiteOpenHelper {
//USER TABLE
public static final String USER_TABLE_NAME = "MyTableTwo";
public static final String USER_COLUMN_USER_ID = "id";
public static final String USER_COLUMN_USER_NAME = "Item_Name";
public static final String USER_COLUMN_USER_ADDRESS = "Weight";
public static final String USER_COLUMN_USER_CREATED_AT = "MRP";
public static final String USER_COLUMN_USER_UPDATED_AT = "BARCODE";
public DBHelper(Context context,
String dbName,
Integer version) {
super(context, dbName, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
tableCreateStatements(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
onCreate(db);
}
private void tableCreateStatements(SQLiteDatabase db) {
try {
db.execSQL(
"CREATE TABLE IF NOT EXISTS "
+ USER_TABLE_NAME + "("
+ USER_COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ USER_COLUMN_USER_NAME + " VARCHAR(20), "
+ USER_COLUMN_USER_ADDRESS + " VARCHAR(50), "
+ USER_COLUMN_USER_CREATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ", "
+ USER_COLUMN_USER_UPDATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ")"
);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<MyTableTwo> getMyItems() {
List<MyTableTwo> mySuperList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + USER_TABLE_NAME;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
MyTableTwo myTable = new MyTableTwo();
myTable.itemName = (c.getString(c.getColumnIndex("Item_Name")));
myTable.weight = (c.getString(c.getColumnIndex("Weight")));
myTable.mrp = (c.getString(c.getColumnIndex("MRP")));
myTable.barcod = (c.getString(c.getColumnIndex("BARCODE")));
mySuperList.add(myTable);
c.moveToNext();
}
}
return mySuperList;
}
}