Reputation: 103
I have a Blob in an SQLite-Database. When I type:
Select length(blobblob) FROM Database WHERE id=9387134648;
I get a size of 20519 bytes (quite big), but when I fetch the Blob with android:
Cursor cursor = database.rawQuery("Select ? FROM Database WHERE id=?;",
new String[]{"blobblob", "9387134648"});
if (cursor.moveToNext()) {
byte[] bytes = cursor.getBlob(0);
Log.v("Bloblength", String.valueOf(bytes.length));
}
I just get a size of 9 bytes.
What do I do wrong?
Upvotes: 1
Views: 1873
Reputation: 434615
I'd guess that you're getting the string "blobblob"
(as an array of bytes) out of your query, those eight characters plus a null terminator would be nine bytes. When you say this:
select ? from database where id = ?
Both placeholders will be replaced by values from your arguments and the arguments are String
s so the ?
will be replaced with SQL strings. The result will be a query that is effectively this:
select 'blobblob' from database where id = '9387134648'
and all that query does is select the SQL string literal 'blobblob'
from your table.
The underlying problem is that you can't use placeholders for SQL identifiers (table names, column names, ...) in a query, placeholders are only for values like strings and numbers. Your code should look more like this:
Cursor cursor = database.rawQuery("Select blobblob FROM Database WHERE id=?;",
new String[]{"9387134648"});
I'm not much of a Java or Android guy so hopefully I haven't butchered the code.
Upvotes: 1