How to insert a binary file into the database of sqlite in C++?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to insert a binary file into the database of sqlite in C++?

ZSP747
I have search with Google , and find these information as below

sqlite3_prepare(..., "insert into foo values(?);", -1, &stmt, ...);
sqlite3_bind_blob(stmt, 1, "bar", 3, SQLITE_TRANSIENT);
sqlite3_step(stmt);

But if there is a file in this path "C:\a.mp3",
I use
"ifstream mp3("C:\\a.mp3")
open this file .

Then I don't know how can I insert it  to the database with the
pointer "sqlite3 *db; "
(the database has two column,
the first's type is text(to write the name of file)
;
the second's type is blob(to write the binary file ) )

Would you please give me some code about this problem?Thank you.

And another question,
What's the lastest parameter's mean of sqlite3_prepare?
I have read the help of this problem , but I can't understand.
--
I am a Chinese,I like program.
Reply | Threaded
Open this post in threaded view
|

Re: How to insert a binary file into the database of sqlite in C++?

Dennis Cote
????? wrote:

>I have search with Google , and find these information as below
>
>sqlite3_prepare(..., "insert into foo values(?);", -1, &stmt, ...);
>sqlite3_bind_blob(stmt, 1, "bar", 3, SQLITE_TRANSIENT);
>sqlite3_step(stmt);
>
>But if there is a file in this path "C:\a.mp3",
>I use
>"ifstream mp3("C:\\a.mp3")
>open this file .
>
>Then I don't know how can I insert it  to the database with the
>pointer "sqlite3 *db; "
>(the database has two column,
>the first's type is text(to write the name of file)
>;
>the second's type is blob(to write the binary file ) )
>
>Would you please give me some code about this problem?Thank you.
>
>And another question,
>What's the lastest parameter's mean of sqlite3_prepare?
>I have read the help of this problem , but I can't understand.
>  
>
This (or something like it) should do what you want, but I'm not sure if
putting large objects like MP3 files into the database is really a good
idea. Others have said they had better performance using the file system
to store large files and simply saving the file names in the database.

ifstream mp3("C:\\a.mp3");
mp3.seekg(0, ios::end);
long sz = mp3.tellg();
mp3.seekg(0, ios::beg);
stringstream sbuf;
sbuf << mp3.rdbuf();
char* buf = sbuf.str();

sqlite3_prepare(..., "insert into foo values(?);", -1, &stmt, ...);
sqlite3_bind_blob(stmt, 1, buf, sz, SQLITE_TRANSIENT);
sqlite3_step(stmt);

HTH
Dennis Cote