Re: [sqlite] using SQLite for binding blobs.

2008-02-15 Thread C S
sorry that should have been insert not insert2, sorry
for the confusion.

what do you mean no parameters in SQL? 

again sorry for sounding noobish -- well fact of the
matter is i am. ive learned as much as i have in  2
days so thanks for the patience and the string
suggestion. 

--- Igor Tandetnik <[EMAIL PROTECTED]> wrote:

> "C S" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > insert << "insert into Images values(NULL, '" <<
> Hello
> > World << "')"
> 
> As predicted, you don't have any parameters in your
> SQL, so there's 
> nothing to bind. It's also not clear why you use a
> stream. You probably 
> want something like
> 
> string insert =
> "insert into Images(imageID, messageID) values
> (?, 'Hello World');";
> 
> Question mark '?' is the parameter placeholder.
> 
> > long ID = db.performUpdate(insert2.str(),
> blobarray,
> > 10);
> 
> What's the relationship between variables named
> 'insert' and 'insert2'? 
> You initialize one, but then use the other.
> 
> Igor Tandetnik 
> 
> 
> 
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 



  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] using SQLite for binding blobs.

2008-02-15 Thread C S
Ken,

  thanks for your response. for cross platform, both
windows and linux are little endian and Macs now use
intel chips, so that is not an issue. i was just
making sure everyone knew i wasnt just developing for
windows. 

about storing them as shorts -- i was under the
impression(at first) that sqlite wanted then data as
bytes but in my code that i just sent, i changed it
via a suggestion from Fin Springs(thanks again). 

please take a look at my code and tell me what is
wrong. i just got thrown into this sql stuff and
trying to learn it on the fly here. much appreciated. 


--- Ken <[EMAIL PROTECTED]> wrote:

> In addition to what Igor mentions:
> 
> If you want this array to be cross platform portable
> from Big endian to Little endian you either need to
> store some type of endianness flag.
> 
> If you dig into the sqlite source you'll find a
> function like sqlite3_put_varint.
> 
> It will convert 32 bit or 64 bit unsinged integers
> into a portable byte string.
> returning the number of bytes back that the string
> consumes. 
> You could then create a struct/type as follows
> struct {
> uint8_t  bytes;
> uint8_t  buf[3];
> }  vint;
> 
> vint.bytes = sqlite3_put_varint (, 
> yourShort );
> 
> then write the entire vint to the BLOB...
> 
> Retrieve the blob as normal. Then unload the shorts
> use the invers function.
> 
> But this seems like a lot of extra work to me.
> 
> Why not create a table that has the integer value
> and insert the data? One row per array element 
> 
> HTH
> 
> C S <[EMAIL PROTECTED]> wrote: hi all i have a
> question regarding Blobs, that is
> storing images into the database. 
> 
> my image by default is an unsigned short array and
> to
> bind blobs it wants a byte array. i am not sure i am
> doing this right at all. is there a universal cross
> platform way for this easy conversion? 
> 
> so far the code i have is:
> .
> char *byteArray = (char *) malloc((size * 2) *
> sizeof(char));
> .
> 
> for(unsigned int i = 0; i < index < size; i++)
> {
>  byteArray[2 * index] = (char)buffer[index];
>  byteArray[(2 * index) + 1] = ( (char) buffer[index]
> >> 1);
> 
> then i prepare the statement which is successful
> then:
> 
> status = sqlite3_bind_blob(statement, 1, byteArray,
> size * 2, free);
> 
> some questions i have: i get an error of '25' back
> from status and looking on the sqlite documention it
> says the 2nd parameter to sql bind was out of range.
> i
> have no idea how the 2nd parameter can be out of
> range. 
> 
> my next question is once you have the blob in the
> database how in the world do you read it back out?
> of
> course when i do read it back out i will need to
> convert it back to a short array to be able to use
> it.
> 
> 
> i am using C++(g++ compiler) on linux. thanks in
> advance! 
> 
> 
>  
>

> Never miss a thing.  Make Yahoo your home page. 
> http://www.yahoo.com/r/hs
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 



  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] using SQLite for binding blobs.

2008-02-15 Thread C S
this is to all who have responded so far: thanks so
much for your help so far: as a caveat, i am new to
SQL so bear with me..

here is my code from the main.cpp where i build the
short array: 

ostringstream insert;

unsigned short *blobarray = new unsigned short[10];

//for simple purposes i will make this a small array. 
for(int i = 0; i < 10; i++)
  blobArray[i] = i * 3;

insert << "insert into Images values(NULL, '" << Hello
World << "')"

long ID = db.performUpdate(insert2.str(), blobarray,
10);

...outside of main.

long performUpdate(string update, const unsigned short
* buffer, unsigned long size)
{
  sqlite3_stmt *statement;
  open(filename); //this simply opens the db file,
nothing big
  
  int status = sqlite3_prepare_v2(db, update.c_str(),
-1, , NULL);

  if((status != SQLITE_OK) || (statement == NULL))
cout << "error preparing" << endl;
  else cout << "preparing SQL statement successful" <<
endl;

  status = sqlite3_bind_blob(statement, 1, buffer,
size * sizeof(unsigned short), SQLITE_STATIC);

  if(status != SQLITE_OK)
cout << "status: " << status << " error binding
blob. " << endl;
  else cout << "binding blob successful" << endl;


  while((status = sqlite3_step(statement)) ==
SQLITE_ROW);
  //check for status error like above

  status = sqlite3_finalize(statement);
  //check for status error like above

//  other non sqlite stuff..

} //end performUpdate


do i need to create a column in table Images for the
blob? or does this happen automatically? right now i
have:

create tables Images(ImageID integer primary key,
 messageID text not null);

do i have to anything here for the blob?

i might have to come back to understand more about the
casting but right now i just wanted to show you my
code. i hope it is readable and once again thanks for
your help in advance! 





--- Fin Springs <[EMAIL PROTECTED]> wrote:

> 
> On Feb 15, 2008, at 5:40 PM, C S usmsci-at-yahoo.com
> |sqlite| wrote:
> 
> > hi all i have a question regarding Blobs, that is
> > storing images into the database.
> >
> > my image by default is an unsigned short array and
> to
> > bind blobs it wants a byte array. i am not sure i
> am
> > doing this right at all. is there a universal
> cross
> > platform way for this easy conversion?
> >
> > so far the code i have is:
> > .
> > char *byteArray = (char *) malloc((size * 2) *
> > sizeof(char));
> > .
> >
> > for(unsigned int i = 0; i < index < size; i++)
> > {
> > byteArray[2 * index] = (char)buffer[index];
> > byteArray[(2 * index) + 1] = ( (char)
> buffer[index]
> >>> 1);
> >
> There is probably no reason to copy your data into a
> separate char  
> array. Presuming you have something like:
> 
> unsigned short myImage[];
> size_t size; // the number of elements in your
> myImage array
> 
> - you can just do:
> 
> status = sqlite3_bind_blob(statement, 1, myImage,
> size *  
> sizeof(unsigned short), SQLITE_STATIC or
> SQLITE_TRANSIENT)
> 
> Your choice for the last parameter will depend on
> the life time of  
> 'myImage'; see
> http://sqlite.org/c3ref/bind_blob.html
> 
> > then i prepare the statement which is successful
> then:
> >
> > status = sqlite3_bind_blob(statement, 1,
> byteArray,
> > size * 2, free);
> >
> > some questions i have: i get an error of '25' back
> > from status and looking on the sqlite documention
> it
> > says the 2nd parameter to sql bind was out of
> range. i
> > have no idea how the 2nd parameter can be out of
> > range.
> You would need to share your statement definition.
> Is there at least  
> one '?' token in it?
> >
> >
> > my next question is once you have the blob in the
> > database how in the world do you read it back out?
> of
> > course when i do read it back out i will need to
> > convert it back to a short array to be able to use
> it.
> >
> Use sqlite3_column_blob to get the pointer and
> sqlite3_column_bytes to  
> know how much data there is (in bytes). You can just
> cast the 'const  
> void *' pointer that you get back to a 'const
> unsigned short *' and  
> divide the number of bytes there is by
> sizeof(unsigned short) (ie 2)  
> to get the number of elements.
> 
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 



  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] using SQLite for binding blobs.

2008-02-15 Thread Ken
In addition to what Igor mentions:

If you want this array to be cross platform portable from Big endian to Little 
endian you either need to store some type of endianness flag.

If you dig into the sqlite source you'll find a function like 
sqlite3_put_varint.

It will convert 32 bit or 64 bit unsinged integers into a portable byte string.
returning the number of bytes back that the string consumes. 
You could then create a struct/type as follows
struct {
uint8_t  bytes;
uint8_t  buf[3];
}  vint;

vint.bytes = sqlite3_put_varint (,  yourShort );

then write the entire vint to the BLOB...

Retrieve the blob as normal. Then unload the shorts use the invers function.

But this seems like a lot of extra work to me.

Why not create a table that has the integer value and insert the data? One row 
per array element 

HTH

C S <[EMAIL PROTECTED]> wrote: hi all i have a question regarding Blobs, that is
storing images into the database. 

my image by default is an unsigned short array and to
bind blobs it wants a byte array. i am not sure i am
doing this right at all. is there a universal cross
platform way for this easy conversion? 

so far the code i have is:
.
char *byteArray = (char *) malloc((size * 2) *
sizeof(char));
.

for(unsigned int i = 0; i < index < size; i++)
{
 byteArray[2 * index] = (char)buffer[index];
 byteArray[(2 * index) + 1] = ( (char) buffer[index]
>> 1);

then i prepare the statement which is successful then:

status = sqlite3_bind_blob(statement, 1, byteArray,
size * 2, free);

some questions i have: i get an error of '25' back
from status and looking on the sqlite documention it
says the 2nd parameter to sql bind was out of range. i
have no idea how the 2nd parameter can be out of
range. 

my next question is once you have the blob in the
database how in the world do you read it back out? of
course when i do read it back out i will need to
convert it back to a short array to be able to use it.


i am using C++(g++ compiler) on linux. thanks in
advance! 


  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] using SQLite for binding blobs.

2008-02-15 Thread Fin Springs

On Feb 15, 2008, at 5:40 PM, C S usmsci-at-yahoo.com |sqlite| wrote:

> hi all i have a question regarding Blobs, that is
> storing images into the database.
>
> my image by default is an unsigned short array and to
> bind blobs it wants a byte array. i am not sure i am
> doing this right at all. is there a universal cross
> platform way for this easy conversion?
>
> so far the code i have is:
> .
> char *byteArray = (char *) malloc((size * 2) *
> sizeof(char));
> .
>
> for(unsigned int i = 0; i < index < size; i++)
> {
> byteArray[2 * index] = (char)buffer[index];
> byteArray[(2 * index) + 1] = ( (char) buffer[index]
>>> 1);
>
There is probably no reason to copy your data into a separate char  
array. Presuming you have something like:

unsigned short myImage[];
size_t size; // the number of elements in your myImage array

- you can just do:

status = sqlite3_bind_blob(statement, 1, myImage, size *  
sizeof(unsigned short), SQLITE_STATIC or SQLITE_TRANSIENT)

Your choice for the last parameter will depend on the life time of  
'myImage'; see http://sqlite.org/c3ref/bind_blob.html

> then i prepare the statement which is successful then:
>
> status = sqlite3_bind_blob(statement, 1, byteArray,
> size * 2, free);
>
> some questions i have: i get an error of '25' back
> from status and looking on the sqlite documention it
> says the 2nd parameter to sql bind was out of range. i
> have no idea how the 2nd parameter can be out of
> range.
You would need to share your statement definition. Is there at least  
one '?' token in it?
>
>
> my next question is once you have the blob in the
> database how in the world do you read it back out? of
> course when i do read it back out i will need to
> convert it back to a short array to be able to use it.
>
Use sqlite3_column_blob to get the pointer and sqlite3_column_bytes to  
know how much data there is (in bytes). You can just cast the 'const  
void *' pointer that you get back to a 'const unsigned short *' and  
divide the number of bytes there is by sizeof(unsigned short) (ie 2)  
to get the number of elements.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] using SQLite for binding blobs.

2008-02-15 Thread Igor Tandetnik
C S <[EMAIL PROTECTED]> wrote:
> hi all i have a question regarding Blobs, that is
> storing images into the database.
>
> my image by default is an unsigned short array and to
> bind blobs it wants a byte array. i am not sure i am
> doing this right at all. is there a universal cross
> platform way for this easy conversion?

unsigned short* image;
char* blob = reinterpret_cast(image);

> so far the code i have is:
> .
> char *byteArray = (char *) malloc((size * 2) *
> sizeof(char));

Curious that you bothered to write sizeof(char), which is always 1 by 
definition, but just hardcoded 2 instead of sizeof(unsigned short).

Anyway, you don't need to allocate new memory or do any copying.

> then i prepare the statement which is successful then:
>
> status = sqlite3_bind_blob(statement, 1, byteArray,
> size * 2, free);
>
> some questions i have: i get an error of '25' back
> from status and looking on the sqlite documention it
> says the 2nd parameter to sql bind was out of range. i
> have no idea how the 2nd parameter can be out of
> range.

You don't have any parameter placeholders in your statement. Show your 
sqlite3_prepare call, in particular what SQL you pass to it.

> my next question is once you have the blob in the
> database how in the world do you read it back out?

sqlite3_column_blob

Igor Tandetnik 



___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] using SQLite for binding blobs.

2008-02-15 Thread C S
hi all i have a question regarding Blobs, that is
storing images into the database. 

my image by default is an unsigned short array and to
bind blobs it wants a byte array. i am not sure i am
doing this right at all. is there a universal cross
platform way for this easy conversion? 

so far the code i have is:
.
char *byteArray = (char *) malloc((size * 2) *
sizeof(char));
.

for(unsigned int i = 0; i < index < size; i++)
{
 byteArray[2 * index] = (char)buffer[index];
 byteArray[(2 * index) + 1] = ( (char) buffer[index]
>> 1);

then i prepare the statement which is successful then:

status = sqlite3_bind_blob(statement, 1, byteArray,
size * 2, free);

some questions i have: i get an error of '25' back
from status and looking on the sqlite documention it
says the 2nd parameter to sql bind was out of range. i
have no idea how the 2nd parameter can be out of
range. 

my next question is once you have the blob in the
database how in the world do you read it back out? of
course when i do read it back out i will need to
convert it back to a short array to be able to use it.


i am using C++(g++ compiler) on linux. thanks in
advance! 


  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users