Advertisement Jump to content
Sign in to follow this  
gretty

Insert Image into SQLite3 Database: Unsure whats causing the error

This topic is 1881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

 

Does anyone have experience inserting binary data into a SQlite3 database using the SQLite3 C API? Note I am using the SQLite3 C API functions imported into a C# application so I am writting C#. I am attempting to write a png to a SQLite3 database in C#. I have managed to correctly import the external DLL function sqlite3_bind_blob. But now I am getting an error when I write the image to my SQLite3 database. When I call the function sqlite3_bind_blob I get the error:

 

#define SQLITE_MISUSE      21   /* Library used incorrectly */

 

I am unsure how I am causing this error and what I can do to fix it. Can you tell me what I am doing wrong?

Heres my code:

// Include dll function (unsure if the parameter types are correct)
[DllImport("sqlite3", EntryPoint = "sqlite3_bind_blob", CallingConvention = CallingConvention.Cdecl)]
private static extern int sqlite3_bind_blob (IntPtr stmHandle, int iIndex, byte[] iParam, int iBytes, IntPtr iOperation);

// Code inside my function: Insert image into database
string query = "INSERT OR REPLACE INTO myTable(lat, lon, image) VALUES(-33.555, 151.457, ?1);";
    
if (sqlite3_prepare_v2 (_connection, query, query.Length, out stmHandle, IntPtr.Zero) != SQLITE_OK) {
    IntPtr errorMsg = sqlite3_errmsg (_connection);
    throw new SqliteException (Marshal.PtrToStringAnsi (errorMsg));
}

IntPtr SQLITE_TRANSIENT = new IntPtr(-1); // Represents SQLITE_TRANSIENT
int res = sqlite3_bind_blob (stmHandle, 1, blob, blob.Length, SQLITE_TRANSIENT);
// res always equals 21

From my debugging I know that the blob correctly contains valid png data because I can write it out to a file and open that file. I also know that the length of the blob is correct aswell.

Edited by gretty

Share this post


Link to post
Share on other sites
Advertisement

Hi, can you inform me where I am misusing the function/s? Are you referring to sqlite3_bind_blob and the 3rd parameter that I am importing as a Byte[] because I questioned this myself but someone advised me to use byte[] instead of a string or etc?

Share this post


Link to post
Share on other sites
First of all, SQLite doesn't care whether your blob is a valid PNG image.

Second, please try to understand what you are doing.
"Someone advised me to use byte[] instead of a string or etc" is not a matter of opinion: the function you are trying to call is declared as int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)), you cannot pass what you want as a parameter.

Third, RTFM.
Why are you goofing around with DllImport instead of using the proper .Net wrapper (system.data.sqlite.org)? Code examples like http://stackoverflow.com/questions/625029/how-do-i-store-and-retrieve-a-blob-from-sqlite are easy to find.

Share this post


Link to post
Share on other sites

First of all, SQLite doesn't care whether your blob is a valid PNG image.

Second, please try to understand what you are doing.
"Someone advised me to use byte[] instead of a string or etc" is not a matter of opinion: the function you are trying to call is declared as int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)), you cannot pass what you want as a parameter.

Third, RTFM.
Why are you goofing around with DllImport instead of using the proper .Net wrapper (system.data.sqlite.org)? Code examples like http://stackoverflow.com/questions/625029/how-do-i-store-and-retrieve-a-blob-from-sqlite are easy to find.

 

You're pretty rude and thats putting it politely. Dont assume I haven't read the ******* manual because I have (I am aware of a much simpler way to achieve what I am trying to do, ie, use system.data.sqlite). I am using Unity3d where you cant use system.data.sqlite.org so you are forced to use the C API, this may have changed in the latest release of Unity3d though.

 

I'll accept that you (incorrectly) assumed my ignorance but I dont accept your aggressive condescending demeanour. You were never obliged to reply to my thread so there's no reason to be angry and I'm telling you, dont reply to my threads in the future.

Edited by gretty

Share this post


Link to post
Share on other sites
From the SQLite web site there are four likely causes:

1. Calling any API routine with an sqlite3* pointer that was not obtained from sqlite3_open() or sqlite3_open16() or which has already been closed by sqlite3_close().

2. Trying to use the same database connection at the same instant in time from two or more threads.

3. Calling sqlite3_step() with a sqlite3_stmt* statement pointer that was not obtained from sqlite3_prepare() or sqlite3_prepare16() or that has already been destroyed by sqlite3_finalize().

4. Trying to bind values to a statement (using sqlite3_bind_...()) while that statement is running.


Your code block isn't big enough to rule out any of these, so look for each of them.

Share this post


Link to post
Share on other sites

You're pretty rude and thats putting it politely. Dont assume I haven't read the ******* manual because I have (I am aware of a much simpler way to achieve what I am trying to do, ie, use system.data.sqlite). I am using Unity3d where you cant use system.data.sqlite.org so you are forced to use the C API, this may have changed in the latest release of Unity3d though.

I'm glad that you aren't as clueless as you originally appeared, but you cannot expect clairvoyance; your post is a great example of how to give a very bad impression without the usual basic mistakes of asking for help (insufficient detail, vague questions, not trying reasonably hard, etc.).

You didn't state that you have a valid reason to use the C API (using Unity 3D), your mentioning that your blob is a valid PNG image and that you tried different data types is more easily explained by assuming grave incompetence than by uncommonly poor writing, and your code example is just incomplete and edited enough to expect the worst from your actual program (see frob's suggestions for a starting point).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!