-1

I have got a an image path stored in a string:

if(uri != null) {
              Cursor cursor = getContentResolver().query(uri, new String[] {   
                                       android.provider.MediaStore.Images.ImageColumns.DATA}, 
                                       null, null, null);
                    cursor.moveToFirst();
                    String image = cursor.getString(0);             
                    cursor.close();

How can I insert/ retrieve this string into/ from this database?

private static final String DATABASE_CREATE =
    "create table " + DATABASE_TABLE + " ("
            + KEY_ROWID + " integer primary key autoincrement, "
            + KEY_IMAGE + " text not null)"; 

1 Answers1

1

You should use ContentValues instance and insert() method of SQLiteDatabase.

ContentValues cv = new ContentValues();
cv.put(<column>, <data>);   
cv.put(<column>, <data>);
cv.put(<column>, <data>);       
db.insert(<tableName>, <columnHackNull>, cv);

This way is generally recommended and clean.

EDIT:

For retrieve data from database, basic example:

String SELECT_QUERY = "SELECT * FROM Table";
Cursor c = db.rawQuery(SELECT_QUERY , null);

or with whereArgs

String SELECT_QUERY = "SELECT * FROM Table WHERE id = ?";
String[] whereArgs = {"<data>","<data>"};
Cursor c = db.rawQuery(SELECT_QUERY , whereArgs );
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • `String SELECT_QUERY = "SELECT * FROM Table"` then `Cursor c = db.rawQuery(SELECT_QUERY , null)` then you maybe want to add data to `listView` so you can use `SimpleCursorAdapter` for example. – Simon Dorociak May 31 '12 at 11:34
  • @Sajmon why not `c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_IMAGE}, null /*String selection*/, null /*String[] selectionArgs*/, null/*String groupBy*/, null/*String having*/, null/*String orderBy*/);` – Selvin May 31 '12 at 12:02
  • @Selvin also this way is correct :-) i wrote basic example, yeah method query is more complex – Simon Dorociak May 31 '12 at 12:22