Sunday, 2 June 2013

Extract/Query/Read SQLite database row by ID

Extract/Query/Read SQLite database row by ID

I created a list, and i want to extract all data at specific id, This is what i tried, but i cant get it to work:
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;
        }
By debugging, i can see the data in "cursor" being:
android.database.sqlite.SQLiteCursor@41e7a1c8
This i hoped would be a complete string of the other data at that id
This is my database:

And here is some more database info:
Here is the activity to call the method:
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);

    }
And here is my databasehandler:
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

No comments:

Post a Comment