gpt4 book ai didi

java - 即使项目存在且属性正确,为什么我的光标为空?

转载 作者:行者123 更新时间:2023-11-30 05:12:04 25 4
gpt4 key购买 nike

所以我一直在尝试查询表中的一些项目,但没有返回任何内容。我知道我的表中只有 3 个项目,但我的选择是有效属性,因此我可以得到一些东西。

表中我的工艺有五个属性,如果一个属性不存在,则表中为 0。我正在做类似“工艺品必须有 2,3,0,0,0 的查询,因为最后三个属性是 0。

但是,即使我的选择存在于表中,也没有返回任何内容。我不确定它为什么要这样做。

我将不胜感激任何帮助!

我的代码在这一行下面:

// 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();
}

最佳答案

My crafts in the table have five attributes on them and if an attribute don't exist, it will be 0



我怀疑您可能做出了不正确的假设,而是当属性不存在时插入的值为 null 并且使用 DEFAULT 0 定义属性列可能会解决问题

有几个因素可能会影响这一点;
  • 列定义
  • 列定义是否具有特定的默认值(DEFAULT 0),而不是默认的 DEFAULT 值为 null。
  • 插入方法
  • 当未分配值时,您是否跳过未分配的值

  • 否则你的代码可以工作,除非当你得到 时没有提取任何行。 android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0 因为您尝试在光标中没有数据时获取数据。
  • 即将光标移动到第一行,当没有行时返回 false(即未移动),然后检查光标是否为空。
  • 当从 SQLiteDatabase 方法(如 rawQuery)返回时,Cursor 永远不会为空。
  • 游标将始终存在并被实例化(因此不为空)并且有效
  • Cursor 将有一个行数(Cursor getCount() 方法返回 Cursor 中行数的整数)。

  • 如果未分配的值设置为 0,您的查询将按预期工作。

    以下代码:-
  • 表明,除了 OutofBounds 问题之外,如果插入 0 并省略属性,您的代码和查询如图所示有效,但如果插入空值,则不然。
  • 允许您查看表格的内容以及是否为 null 或 0
  • 显示当属性被省略而不是专门设置为 0 时使用 DEFAULT 0 所产生的差异。
  • 使用第二个表,不同之处仅在于属性列定义中编码了 DEFAULT 0。

  • 数据库助手 (使用 insert 方法和 dumpTable 方法):-
    public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "mydb";
    public static final int DBVERSION = 1;

    public static final String TABLE_NAME = "mytable";
    public static final String TABLE_NAME2 = "mytable2";
    public static final String UID = "uid";
    public static final String CNAME = "cname";
    public static final String FIRST_ATTRIBUTE = "a1";
    public static final String SECOND_ATTRIBUTE = "a2";
    public static final String THIRD_ATTRIBUTE = "a3";
    public static final String FOURTH_ATTRIBUTE = "a4";
    public static final String FIFTH_ATTRIBUTE = "a5";

    SQLiteDatabase mDB;

    public DBHelper(Context context) {
    super(context, DBNAME, null, DBVERSION);
    mDB = this.getWritableDatabase();
    }

    // Create both tables
    @Override
    public void onCreate(SQLiteDatabase db) {

    String crt_tbl = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
    UID + " INTEGER PRIMARY KEY," +
    CNAME + " TEXT, " +
    FIRST_ATTRIBUTE + " INTEGER, " +
    SECOND_ATTRIBUTE + " INTEGER," +
    THIRD_ATTRIBUTE + " INTEGER, " +
    FOURTH_ATTRIBUTE + " INTEGER, " +
    FIFTH_ATTRIBUTE + " INTEGER" +
    ")";
    db.execSQL(crt_tbl);

    String crt_tbl2 = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME2 + "(" +
    UID + " INTEGER PRIMARY KEY," +
    CNAME + " TEXT, " +
    FIRST_ATTRIBUTE + " INTEGER DEFAULT 0, " +
    SECOND_ATTRIBUTE + " INTEGER DEFAULT 0," +
    THIRD_ATTRIBUTE + " INTEGER DEFAULT 0, " +
    FOURTH_ATTRIBUTE + " INTEGER DEFAULT 0, " +
    FIFTH_ATTRIBUTE + " INTEGER DEFAULT 0" +
    ")";
    db.execSQL(crt_tbl2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    }

    // Generic Insert method
    public long insertRow(String table, String cname, int[] attributes) {
    String[] attribute_columns = new String[]{FIRST_ATTRIBUTE,SECOND_ATTRIBUTE,THIRD_ATTRIBUTE,FOURTH_ATTRIBUTE,FIFTH_ATTRIBUTE};
    ContentValues cv = new ContentValues();
    cv.put(CNAME,cname);
    for (int i=0; i < attributes.length; i++) {
    cv.put(attribute_columns[i],attributes[i]);
    }
    return mDB.insert(table,null,cv);
    }

    // Insert row into table 1 with all 5 attributes
    public long insertRow(String cname,int a1, int a2, int a3, int a4, int a5) {
    int[] attributes = new int[]{a1,a2,a3,a4,a5};
    return insertRow(TABLE_NAME,cname,attributes);
    }

    // Inert row in table 1 with first 2 attributes
    public long insertRow(String cname, int a1, int a2) {
    int[] attributes = new int[]{a1,a2};
    return insertRow(TABLE_NAME,cname,attributes);
    }

    // Insert row into table 2 with all 5 atributes
    public long insertRow2(String cname,int a1, int a2, int a3, int a4, int a5) {
    int[] attributes = new int[]{a1,a2,a3,a4,a5};
    return insertRow(TABLE_NAME2,cname,attributes);
    }

    // Insert row into table 2 with first 2 attributes
    public long insertRow2(String cname, int a1, int a2) {
    int[] attributes = new int[]{a1,a2};
    return insertRow(TABLE_NAME2,cname,attributes);
    }

    public void dumpTableToLog(String table) {
    Log.d("DUMPTABLE","Dumping table ==>" + table);
    Cursor csr = mDB.query(table,null,null,null,null,null,null);
    DatabaseUtils.dumpCursor(csr);
    csr.close();
    }
    }

    调用 Activity (MainActivity),包括您的 获取数据 和另一种 getData2 方法:-
    public class MainActivity extends AppCompatActivity {

    DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dbHelper = new DBHelper(this);

    //Example table 1 load data

    dbHelper.getWritableDatabase().delete(DBHelper.TABLE_NAME,null,null); // Empty table
    dbHelper.insertRow("Crafts", 2, 3, 0, 0, 0);
    dbHelper.insertRow("Arts", 1, 1, 1, 1, 1);
    dbHelper.insertRow("Science", 6, 3, 0, 0, 0);
    dbHelper.insertRow("Maths", 2, 3, 0, 0, 0);
    dbHelper.insertRow("Crafts2",2,3); //<<<<<<<<<< NULLS for a3-a5
    dbHelper.dumpTableToLog(DBHelper.TABLE_NAME); // Dump all table 1 contents to the Log

    Log.d("GETDATAREUSULT", getData(2, 3, 0, 0, 0));
    Log.d("GETDATAREUSULT", getData(1, 3, 0, 0, 0));

    //Example table 2 (with DEFAULT 0) (uses methods ending with 2)

    dbHelper.getWritableDatabase().delete(DBHelper.TABLE_NAME,null,null); // Empty table
    dbHelper.insertRow2("Crafts", 2, 3, 0, 0, 0);
    dbHelper.insertRow2("Arts", 1, 1, 1, 1, 1);
    dbHelper.insertRow2("Science", 6, 3, 0, 0, 0);
    dbHelper.insertRow2("Maths", 2, 3, 0, 0, 0);
    dbHelper.insertRow2("Crafts2",2,3); //<<<<<<<<<< NULLS for a3-a5
    dbHelper.dumpTableToLog(DBHelper.TABLE_NAME2); // Dump all table 2 contents to the Log

    Log.d("GETDATA2REUSULT", getData2(2, 3, 0, 0, 0));
    Log.d("GETDATA2REUSULT", getData2(1, 3, 0, 0, 0));
    }


    // 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();
    while (cursor.moveToNext()) {
    buffer.append(cursor.getString(cursor.getColumnIndex(DBHelper.CNAME))).append(" ");
    }
    cursor.close(); //<<<<<<<<<< Should always close cursor when done with it.


    /*
    cursor.moveToFirst(); //<<<<<<<<<< SHOULD ALWAYS CHECK result of move false if unable true if able to move

    if (cursor != null) { //<<<<<<<<<< A cursor return from SQLiteDatabase meyhod will never be null, useless/dangerous

    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();
    }

    /**
    * Alternative using the convenience query method
    * @param firstSelection
    * @param secondSelection
    * @param thirdSelection
    * @param fourthSelection
    * @param fifthSelection
    * @return
    */
    public String getData2(int firstSelection, int secondSelection, int thirdSelection,
    int fourthSelection, int fifthSelection) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String whereclause = DBHelper.FIRST_ATTRIBUTE + "=? AND " +
    DBHelper.SECOND_ATTRIBUTE + "=? AND " +
    DBHelper.THIRD_ATTRIBUTE + "=? AND " +
    DBHelper.FOURTH_ATTRIBUTE + "=? AND " +
    DBHelper.FIFTH_ATTRIBUTE + "=?"
    ;
    String[] whereargs = new String[]{
    String.valueOf(firstSelection),
    String.valueOf(secondSelection),
    String.valueOf(thirdSelection),
    String.valueOf(fourthSelection),
    String.valueOf(fifthSelection)}
    ;
    Cursor cursor = db.query(
    DBHelper.TABLE_NAME2,
    null,
    whereclause,
    whereargs,
    null,null,null
    );
    StringBuilder buffer = new StringBuilder();
    while (cursor.moveToNext()) {
    buffer.append(cursor.getString(cursor.getColumnIndex(DBHelper.CNAME))).append(" ");
    }
    cursor.close(); //<<<<<<<<<< Should always close cursor when done with it.
    return buffer.toString();
    }
    }

    结果 :-

    以下是日志的输出
  • 需要注意的是 第一张表 ( without DEFAULT 0 )不选择最后三个属性为空的最后一行( Crafts2 ),根据 D/GETDATAREUSULT: Crafts Maths ;
  • 而对于第二张表 ( 和 DEFAULT 0 )选择最后一行( Crafts2 ),根据 D/GETDATA2REUSULT: Crafts Maths Crafts2
  • DumpTable 显示了 的最后一行。第一张表 (属性 3-5
    作为空值)
    12-01 01:09:12.140 1698-1698/? I/System.out: 4 {
    12-01 01:09:12.140 1698-1698/? I/System.out: uid=5
    12-01 01:09:12.140 1698-1698/? I/System.out: cname=Crafts2
    12-01 01:09:12.140 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.140 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.140 1698-1698/? I/System.out: a3=null
    12-01 01:09:12.140 1698-1698/? I/System.out: a4=null
    12-01 01:09:12.140 1698-1698/? I/System.out: a5=null
    12-01 01:09:12.140 1698-1698/? I/System.out: }
  • 对于第二张表作为
    12-01 01:09:12.180 1698-1698/? I/System.out: 4 {
    12-01 01:09:12.180 1698-1698/? I/System.out: uid=5
    12-01 01:09:12.180 1698-1698/? I/System.out: cname=Crafts2
    12-01 01:09:12.180 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.180 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.180 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.180 1698-1698/? I/System.out: }

  • :-
    12-01 01:09:12.140 1698-1698/? D/DUMPTABLE: Dumping table ==>mytable
    12-01 01:09:12.140 1698-1698/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534b1f2c
    12-01 01:09:12.140 1698-1698/? I/System.out: 0 {
    12-01 01:09:12.140 1698-1698/? I/System.out: uid=1
    12-01 01:09:12.140 1698-1698/? I/System.out: cname=Crafts
    12-01 01:09:12.140 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.140 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.140 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.140 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.140 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.140 1698-1698/? I/System.out: }
    12-01 01:09:12.140 1698-1698/? I/System.out: 1 {
    12-01 01:09:12.140 1698-1698/? I/System.out: uid=2
    12-01 01:09:12.140 1698-1698/? I/System.out: cname=Arts
    12-01 01:09:12.140 1698-1698/? I/System.out: a1=1
    12-01 01:09:12.140 1698-1698/? I/System.out: a2=1
    12-01 01:09:12.140 1698-1698/? I/System.out: a3=1
    12-01 01:09:12.140 1698-1698/? I/System.out: a4=1
    12-01 01:09:12.140 1698-1698/? I/System.out: a5=1
    12-01 01:09:12.140 1698-1698/? I/System.out: }
    12-01 01:09:12.140 1698-1698/? I/System.out: 2 {
    12-01 01:09:12.140 1698-1698/? I/System.out: uid=3
    12-01 01:09:12.140 1698-1698/? I/System.out: cname=Science
    12-01 01:09:12.140 1698-1698/? I/System.out: a1=6
    12-01 01:09:12.140 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.140 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.140 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.140 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.140 1698-1698/? I/System.out: }
    12-01 01:09:12.140 1698-1698/? I/System.out: 3 {
    12-01 01:09:12.140 1698-1698/? I/System.out: uid=4
    12-01 01:09:12.140 1698-1698/? I/System.out: cname=Maths
    12-01 01:09:12.140 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.140 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.140 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.140 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.140 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.140 1698-1698/? I/System.out: }
    12-01 01:09:12.140 1698-1698/? I/System.out: 4 {
    12-01 01:09:12.140 1698-1698/? I/System.out: uid=5
    12-01 01:09:12.140 1698-1698/? I/System.out: cname=Crafts2
    12-01 01:09:12.140 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.140 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.140 1698-1698/? I/System.out: a3=null
    12-01 01:09:12.140 1698-1698/? I/System.out: a4=null
    12-01 01:09:12.140 1698-1698/? I/System.out: a5=null
    12-01 01:09:12.140 1698-1698/? I/System.out: }
    12-01 01:09:12.140 1698-1698/? I/System.out: <<<<<
    12-01 01:09:12.144 1698-1698/? D/GETDATAREUSULT: Crafts Maths
    12-01 01:09:12.176 1698-1698/? D/DUMPTABLE: Dumping table ==>mytable2
    12-01 01:09:12.176 1698-1698/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534a1c1c
    12-01 01:09:12.180 1698-1698/? I/System.out: 0 {
    12-01 01:09:12.180 1698-1698/? I/System.out: uid=1
    12-01 01:09:12.180 1698-1698/? I/System.out: cname=Crafts
    12-01 01:09:12.180 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.180 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.180 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.180 1698-1698/? I/System.out: }
    12-01 01:09:12.180 1698-1698/? I/System.out: 1 {
    12-01 01:09:12.180 1698-1698/? I/System.out: uid=2
    12-01 01:09:12.180 1698-1698/? I/System.out: cname=Arts
    12-01 01:09:12.180 1698-1698/? I/System.out: a1=1
    12-01 01:09:12.180 1698-1698/? I/System.out: a2=1
    12-01 01:09:12.180 1698-1698/? I/System.out: a3=1
    12-01 01:09:12.180 1698-1698/? I/System.out: a4=1
    12-01 01:09:12.180 1698-1698/? I/System.out: a5=1
    12-01 01:09:12.180 1698-1698/? I/System.out: }
    12-01 01:09:12.180 1698-1698/? I/System.out: 2 {
    12-01 01:09:12.180 1698-1698/? I/System.out: uid=3
    12-01 01:09:12.180 1698-1698/? I/System.out: cname=Science
    12-01 01:09:12.180 1698-1698/? I/System.out: a1=6
    12-01 01:09:12.180 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.180 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.180 1698-1698/? I/System.out: }
    12-01 01:09:12.180 1698-1698/? I/System.out: 3 {
    12-01 01:09:12.180 1698-1698/? I/System.out: uid=4
    12-01 01:09:12.180 1698-1698/? I/System.out: cname=Maths
    12-01 01:09:12.180 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.180 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.180 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.180 1698-1698/? I/System.out: }
    12-01 01:09:12.180 1698-1698/? I/System.out: 4 {
    12-01 01:09:12.180 1698-1698/? I/System.out: uid=5
    12-01 01:09:12.180 1698-1698/? I/System.out: cname=Crafts2
    12-01 01:09:12.180 1698-1698/? I/System.out: a1=2
    12-01 01:09:12.180 1698-1698/? I/System.out: a2=3
    12-01 01:09:12.180 1698-1698/? I/System.out: a3=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a4=0
    12-01 01:09:12.180 1698-1698/? I/System.out: a5=0
    12-01 01:09:12.180 1698-1698/? I/System.out: }
    12-01 01:09:12.180 1698-1698/? I/System.out: <<<<<
    12-01 01:09:12.184 1698-1698/? D/GETDATA2REUSULT: Crafts Maths Crafts2

    关于java - 即使项目存在且属性正确,为什么我的光标为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53548696/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com