0

我创建了一个列表,我想提取特定 id 的所有数据,这是我尝试过的,但我无法让它工作:

public Cursor fetchById(long id) throws SQLException {

            Cursor mCursor = mDb.query(true, SQLITE_TABLE, new String[] {
                    KEY_ROWID, KEY_MATCH, KEY_TIME, KEY_BOOKMAKERS, KEY_ODDS1 },
                    KEY_ROWID + "=" + id, null, null, null, null, null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

通过调试,我可以看到“光标”中的数据为:

android.database.sqlite.SQLiteCursor@41e7a1c8

我希望这将是该 ID 处其他数据的完整字符串

这是我的数据库:

在此处输入图像描述

这是更多的数据库信息: 在此处输入图像描述

这是调用该方法的活动:

选择.java

public class Choice extends Activity {

    private BetsDbAdapter dbHelper;
    private SimpleCursorAdapter dataAdapter;
    Long value;
    TextView idshow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choicelayout);

        //Database
        dbHelper = new BetsDbAdapter(this);
        dbHelper.open();

        Bundle extras = getIntent().getExtras();
        if(extras !=null){
             value = extras.getLong("id");
        }

    String id = String.valueOf(value);

        Cursor cursor = dbHelper.fetchById(value);  //Here i call the function     

        idshow = (TextView) findViewById(R.id.textView1);
        idshow.setText(cursor);

    }

这是我的数据库处理程序:

BetsDbAdapter.java

public class BetsDbAdapter {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_MATCH = "match";
    public static final String KEY_TIME = "time";
    public static final String KEY_BOOKMAKERS = "bookmakers";
    public static final String KEY_ODDS1 = "odds1";

    private static final String TAG = "BetsDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_NAME = "Bets.db";
    private static final String SQLITE_TABLE = "BetTable";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
            + SQLITE_TABLE + " (" + KEY_ROWID
            + " integer PRIMARY KEY autoincrement," + KEY_MATCH + ","
            + KEY_TIME + "," + KEY_BOOKMAKERS + "," + KEY_ODDS1 + ","
            + " UNIQUE (" + KEY_MATCH + "));";

    private static class DatabaseHelper extends SQLiteOpenHelper {

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

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE);
        }

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

    public BetsDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public BetsDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        if (mDbHelper != null) {
            mDbHelper.close();
        }
    }
public long createBets(String match, String time, String bookmakers,
            String odds1) {

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_MATCH, match);
        initialValues.put(KEY_TIME, time);
        initialValues.put(KEY_BOOKMAKERS, bookmakers);
        initialValues.put(KEY_ODDS1, odds1);

        return mDb.insert(SQLITE_TABLE, null, initialValues);
    }

public Cursor fetchById(long id) throws SQLException {

        Cursor mCursor = mDb.query(true, SQLITE_TABLE, new String[] {
                KEY_ROWID, KEY_MATCH, KEY_TIME, KEY_BOOKMAKERS, KEY_ODDS1 },
                KEY_ROWID + "=" + id, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
4

2 回答 2

1

您不能将光标用作 setText 的参数。尝试

String text = cursor.getString(cursor.getColumnIndex(BetsDbAdapter.KEY_MATCH)) 
             + "\n" + cursor.getString(cursor.getColumnIndex(BetsDbAdapter.KEY_TIME))
             + "\n" + ....
idshow.setText(text);
于 2013-06-02T21:27:59.417 回答
0

对于您的选择参数,您可能需要用单引号将目标值括起来,如下所示:

KEY_ROWID + "='" + id + "'"

或者,您也可以像这样使用 selectionArgs 参数:

String selection = KEY_ROWID + "=?";
String[] selectionArgs = {id};
Cursor mCursor = mDb.query(true, SQLITE_TABLE, new String[] {
                KEY_ROWID, KEY_MATCH, KEY_TIME, KEY_BOOKMAKERS, KEY_ODDS1 },
                selection, seelctionArgs, null, null, null, null);
于 2013-06-02T21:12:15.827 回答