按ID提取/查询/读取SQLite数据库行

时间:2013-06-02 21:04:03

标签: android sqlite

我创建了一个列表,我想提取特定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

的其他数据的完整字符串

这是我的数据库:

enter image description here

这里有一些更多的数据库信息: enter image description here

以下是调用该方法的活动:

Choice.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;
    }

2 个答案:

答案 0 :(得分:1)

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

String text = cursor.getString(cursor.getColumnIndex(BetsDbAdapter.KEY_MATCH)) 
             + "\n" + cursor.getString(cursor.getColumnIndex(BetsDbAdapter.KEY_TIME))
             + "\n" + ....
idshow.setText(text);

答案 1 :(得分: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);