- Home /
Storing blob in SQLite
Hi, I wrote this code to read blob data from a SQLite DB:
public static byte[] GetBlobData(string Query){
byte[] BlobContent = new byte[0];
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = Query;
reader = dbcmd.ExecuteReader();
while (reader.Read()) {
BlobContent = (byte[])reader["blob"];
}
return BlobContent;
}
Where query is like "SELECT blob FROM 'blobfield' WHERE id="...
How about storing blob data? Any suggestion?
Thanks!
Gius
Answer by Giusort · Mar 14, 2018 at 02:28 PM
So, the correct code is:
public static bool SaveBlobData(String tableName, byte[] BlobContent) {
string query;
query = "INSERT INTO " + tableName + "(blob) VALUES (@BlobContent)";
try {
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
SqliteParameter setParam = new SqliteParameter("@BlobContent", BlobContent);
dbcmd.Parameters.Add(setParam);
dbcmd.ExecuteNonQuery ();
} catch(Exception exception) {
Debug.LogError(exception.Message);
return false;
}
return true;
}
Don't you forget to open the DB before saving data in. Hope this help!
While I can see this working (ie. not throwing an error), it is also not saving the byte[] value to the DB. Debugging setParam shows that it was associated properly, and I can see the array values. Looking at the DB post insertion, that column is empty. Any ideas where the issue might be?
NOTE: Because references for SQLite for C# indicate the need to also set the dbType and size, I've included those
SqliteParameter paramPhoto = new SqliteParameter("@photoBytes", photoBytes);
paramPhoto.Size = photoBytes.Length;
paramPhoto.DbType = DbType.Binary;
cmdCreateUser.Parameters.Add(paramPhoto);
Tried the code both with and without those 2 lines, same result in both cases.
Your answer
Follow this Question
Related Questions
Setting Saved Properties of Procedurally Generated Texture2Ds... 0 Answers
loading image as a blob from Sqlite,loading image from Sqlite 1 Answer
How to save blob in bytes to sqlite 0 Answers
Image showing as question mark after loading from SQLite database 0 Answers
SQLite, BLOB and UI Image 2 Answers