0

I have a SQLiteDatabas and I want to store my byte[] in a field called "Data", the Datetype I'm using at this moment is called: Blob and the method to store the Byte array in the SQLiteDatabase looks like this:

public bool InsertMessage()
{
    //create string SQl and fill it with the SQLite query for inserting a message.
    string SQL = "Insert into ClockMessages(InsertDateTime, SendDateTime, Data) Values('"+ insertdatetime + "', null, '" + data + "');";
    List<SQLiteParameter> pars = new List<SQLiteParameter>();
    pars.Add(new SQLiteParameter("InsertDateTime", insertdatetime));
    pars.Add(new SQLiteParameter("Data", data));
    //send the SQl query and it's parameters to the SQLiteDatabase class
    return SQLiteDatabase.ExecuteNonQuery(SQL, pars);
}

The field Data field in my SQLiteDatabase now contains the following text: System.Byte[]

I want to actually store the real byte[] in the Database, how can I achieve this? Do I need another DateType for the Data field?

3
  • Did you convert an object to the byte-array, or is the byte array your base? Commented Feb 25, 2013 at 9:54
  • I didn't convert anything yet, I want to store complete byte array in the SQLite database, but if needed I can cut down the byte array in single bytes and then store them Commented Feb 25, 2013 at 9:58
  • It looks as though someone has marked your question answered, but I can't find an example here of how to get the data back out of the database after it has been put in. Commented Mar 12, 2014 at 11:50

2 Answers 2

2

You should use parameters in the statement:

string SQL = "INSERT INTO ClockMessages(InsertDateTime, SendDateTime, Data) VALUES (@InsertDateTime, NULL, @Data)";

Everything else seems correct.

Sign up to request clarification or add additional context in comments.

Comments

-1

I would convert the byte-array to a base64string and store it in a nvarchar(max) field

Convert.ToBase64String(byte[])

http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

And revert it by byte[] = Convert.FromBase64String

3 Comments

I'm using SQLite, so I can't use nvarchar(max), should I use the datatype: TEXT ?
Sorry, haven't seen that - should be ok, hence SQLite is generally unicode and size is not restricted
When I revert I get the following error: Invalid length for a Base-64 char array or string. How do you exactly revert?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.