Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
hey i wanted to thank Igor, Ron, Dennis and Matthew
(sorry if i forgot anyone else) with helping me with
this blob stuff.
-cheers

c.s.
--- Igor Tandetnik <[EMAIL PROTECTED]> wrote:

> C S <[EMAIL PROTECTED]> wrote:
> > -so it seems that despite my byte order being
> wrong?,
> > i get the output back out exactly how i stored it
> in
> > the original array.
> 
> There is nothing wrong with the byte order. It
> matches the native order 
> of your CPU.
> 
> > is there a problem here i should
> > be concerned about in the long run or why i should
> > change the order?
> 
> If you ever need to have the database file portable
> between machines 
> with different architecture (e.g. Intel Mac vs
> PowerPC Mac) you will 
> have to think about this endianness problem.
> 
> > i am running on a Linux Ubuntu machine.
> 
> The operating system is largely irrelevant. What's
> important is what 
> hardware you are running on.
> 
> Igor Tandetnik 
> 
> 
> 
> ___
> 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] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Igor Tandetnik
C S <[EMAIL PROTECTED]> wrote:
> -so it seems that despite my byte order being wrong?,
> i get the output back out exactly how i stored it in
> the original array.

There is nothing wrong with the byte order. It matches the native order 
of your CPU.

> is there a problem here i should
> be concerned about in the long run or why i should
> change the order?

If you ever need to have the database file portable between machines 
with different architecture (e.g. Intel Mac vs PowerPC Mac) you will 
have to think about this endianness problem.

> i am running on a Linux Ubuntu machine.

The operating system is largely irrelevant. What's important is what 
hardware you are running on.

Igor Tandetnik 



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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
Ron - 

this is my select statement:
 
sqlite> select imageID, hex(imageData) from Images;
1|0300060009000C000F001200150018001B00
2|010002000300040005000600070008000900
3|010002000300040005000600070008000900

-sorry for the confusion i was just copying and
pasting wrong. looks like everything looks fine if you
were to space these out. 


--- "Wilson, Ron P"
<[EMAIL PROTECTED]> wrote:

> you are delimiting your dwords wrong.  reverse the
> byte order.  this
> resolves your leading and trailing 00 problem.
> 
> sqlite> select imageID, hex(imageData) from Images;
> 1| 0300 0600 0900 0C00 0F00 1200 1500 1800 1B00
> 2| 0100 0200 0300 0400 0500 0600 0700 0800 0900
> 3| 0100 0200 0300 0400 0500 0600 0700 0800 0900
> *** 
> 
> RW
> 
> Ron Wilson, S/W Systems Engineer III, Tyco
> Electronics, 434.455.6453
> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf
> Of C S
> Sent: Wednesday, March 12, 2008 12:07 PM
> To: General Discussion of SQLite Database
> Subject: Re: [sqlite] SQL Blobs - what am i doing
> wrong here?(code
> inside)
> 
> Igor,
> 
>   thanks for all your help too man. here is what is
> in
> the table images so far when i do a select. its 3
> blobs:
> 
> sqlite> select imageID, hex(imageData) from Images;
> 1|00 0003 0006 0009 000C 000F 0012 0015 0018 001B 00
> 2|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
> 3|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
>
*
> 
> here is what i have now:
> 
> const unsigned short *image;
> int i = 0;
> 
> myString2 = "select imageID, imageData from Images";
> 
> int status = sqlite3_prepare_v2(db, update.c_str(),
> -1, , NULL);
> 
>  while ((status = sqlite3_step(statement)) ==
> SQLITE_ROW)
>{
> i++
> 
> if(i == 1)
>   std::cout << "first row" << std::endl;
> 
> else std::cout << "another row" << std::endl;
> 
> num_bytes = sqlite3_column_bytes(statement, i);
> num_bytes = num_bytes / sizeof(unsigned short);
> image = new unsigned short[num_bytes];
> image = (const unsigned
> short*)sqlite3_column_blob(statement, i);
> 
> for(int i = 0; i < num_bytes; i++)
>std::cout << "image " << i << ": " <<
> image[i]
> << std::endl;
> 
> //delete [] image; //causes a crash so far. 
>}
> 
>  status = sqlite3_finalize(statement);
>  if (status != SQLITE_OK)
>  std::cerr << "Error deleting prepared SQL
> statement" << std::endl;
> 
>  else std::cout << "finalized statement
> successfully"
> << std::endl;
> 
> ***
> 
> Output:
> 
> first row
> image 0: 0
> image 1: 3
> image 2: 6
> image 3: 9
> image 4: 12
> image 5: 15
> image 6: 18
> image 7: 21
> image 8: 24
> image 9: 27
> another row
> another row
> finalized statement successfully
> 
> --so for some reason i get the first
> blob(successfully, yes!) but it never increments
> after
> that. the statement is also finalized successfully.
> any reason why the other two blobs are not printing
> out? 
> 
> as a side note i commented out the deleting of the
> image because i got a major abort crash, no doubt
> memory leaks/forbidden access issues. 
> 
> thanks!! 
> 
> 
> 
> 
> 
> 
> --- Igor Tandetnik <[EMAIL PROTECTED]> wrote:
> 
> > C S <[EMAIL PROTECTED]> wrote:
> > >  thanks for the tip on the hex. now i can see my
> > > entry of the blob. my original loop to fill the
> > > dynamic array with unsigned shorts was this:
> > >
> > > imageArray = new unsigned short[10];
> > >
> > > for(int i = 0; i < 10; i++)
> > >  imageArray[i] = i;
> > >
> > > so now if i run the program then do a:
> > >
> > > select imageID, hex(imageData) from Images;
> > >
> > > i get the result:
> > >
> > > 1|010002000300040005000600070008000900
> > >
> > > i am not sure where the first 2 leading zeros
> are
> > > coming from
> > 
> > The first value (the one at index 0) in imageArray
> > is zero, represented 
> > by two zero bytes.
> > 
> > > now i need to be able to extract the data back
> out
> > of
> &g

Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
Igor,

  haha i JUST changed this before i checked your email
to:

while ((status = sqlite3_step(statement)) ==
SQLITE_ROW)
   {
std::cout << "status is: " << status <<
std::endl;
i++;
if(i == 1)
 std::cout << "first row" << std::endl;
else
  std::cout << "another row" << std::endl;
num_bytes = sqlite3_column_bytes(statement,
1);
num_bytes = num_bytes / sizeof(unsigned
short);
image = new unsigned short[num_bytes];
image = (const unsigned
short*)sqlite3_column_blob(statement, 1);

 for(int j = 0; j < num_bytes; j++)
   std::cout << "image " << j << ": " << image[j]
<< std::endl;
**

so i get the output correct back out now: 

Output --

first row
image 0: 0
image 1: 3
image 2: 6
image 3: 9
image 4: 12
image 5: 15
image 6: 18
image 7: 21
image 8: 24
image 9: 27
another row
image 0: 0
image 1: 1
image 2: 2
image 3: 3
image 4: 4
image 5: 5
image 6: 6
image 7: 7
image 8: 8
image 9: 9
another row
image 0: 0
image 1: 1
image 2: 2
image 3: 3
image 4: 4
image 5: 5
image 6: 6
image 7: 7
image 8: 8
image 9: 9
***

-so it seems that despite my byte order being wrong?,
i get the output back out exactly how i stored it in
the original array. is there a problem here i should
be concerned about in the long run or why i should
change the order? 

would like your response -- thanks!

oh yeah - 

i am running on a Linux Ubuntu machine. the only big
endian machines now a days that i can think of are old
SGI's(didnt they switch to intel) and old mac's(which
now use intel(little endian)). 

*


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

> C S <[EMAIL PROTECTED]> wrote:
> >  thanks for all your help too man. here is what is
> in
> > the table images so far when i do a select. its 3
> > blobs:
> >
> > sqlite> select imageID, hex(imageData) from
> Images;
> > 1|00 0003 0006 0009 000C 000F 0012 0015 0018 001B
> 00
> > 2|00 0001 0002 0003 0004 0005 0006 0007 0008 0009
> 00
> > 3|00 0001 0002 0003 0004 0005 0006 0007 0008 0009
> 00
> >
>
*
> 
> I bet you are running on a little-endian machine. On
> such a machine, the 
> low-order byte comes first. So the correct breakdown
> is
> 
> 1| 0300 0600 0900 0C00 0F00 1200 1500 1800 1B00
> 
> For example, two bytes 0300 represent a two-byte
> integer 3 in 
> little-endian (least-significant byte first,
> most-significant last).
> 
> > while ((status = sqlite3_step(statement)) ==
> > SQLITE_ROW)
> >   {
> >i++
> >
> >num_bytes = sqlite3_column_bytes(statement, i);
> 
> 'i' refers to the row index, and is incremented with
> each step. The last 
> parameter to sqlite3_column_bytes is a _column_
> index: it doesn't change 
> as you scan through rows. You always pass 0 to
> retrieve the first column 
> (from the current row, whatever it happens to be), 1
> to retrieve the 
> second column and so on.
> 
> Your code just accidentally happens to pass a
> correct column index for 
> the first row, but wrong index for all subsequent
> rows.
> 
> Igor Tandetnik 
> 
> 
> 
> ___
> 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] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Igor Tandetnik
C S <[EMAIL PROTECTED]> wrote:
>  thanks for all your help too man. here is what is in
> the table images so far when i do a select. its 3
> blobs:
>
> sqlite> select imageID, hex(imageData) from Images;
> 1|00 0003 0006 0009 000C 000F 0012 0015 0018 001B 00
> 2|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
> 3|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
> *

I bet you are running on a little-endian machine. On such a machine, the 
low-order byte comes first. So the correct breakdown is

1| 0300 0600 0900 0C00 0F00 1200 1500 1800 1B00

For example, two bytes 0300 represent a two-byte integer 3 in 
little-endian (least-significant byte first, most-significant last).

> while ((status = sqlite3_step(statement)) ==
> SQLITE_ROW)
>   {
>i++
>
>num_bytes = sqlite3_column_bytes(statement, i);

'i' refers to the row index, and is incremented with each step. The last 
parameter to sqlite3_column_bytes is a _column_ index: it doesn't change 
as you scan through rows. You always pass 0 to retrieve the first column 
(from the current row, whatever it happens to be), 1 to retrieve the 
second column and so on.

Your code just accidentally happens to pass a correct column index for 
the first row, but wrong index for all subsequent rows.

Igor Tandetnik 



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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Wilson, Ron P
you are delimiting your dwords wrong.  reverse the byte order.  this
resolves your leading and trailing 00 problem.

sqlite> select imageID, hex(imageData) from Images;
1| 0300 0600 0900 0C00 0F00 1200 1500 1800 1B00
2| 0100 0200 0300 0400 0500 0600 0700 0800 0900
3| 0100 0200 0300 0400 0500 0600 0700 0800 0900
*** 

RW

Ron Wilson, S/W Systems Engineer III, Tyco Electronics, 434.455.6453
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of C S
Sent: Wednesday, March 12, 2008 12:07 PM
To: General Discussion of SQLite Database
Subject: Re: [sqlite] SQL Blobs - what am i doing wrong here?(code
inside)

Igor,

  thanks for all your help too man. here is what is in
the table images so far when i do a select. its 3
blobs:

sqlite> select imageID, hex(imageData) from Images;
1|00 0003 0006 0009 000C 000F 0012 0015 0018 001B 00
2|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
3|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
*

here is what i have now:

const unsigned short *image;
int i = 0;

myString2 = "select imageID, imageData from Images";

int status = sqlite3_prepare_v2(db, update.c_str(),
-1, , NULL);

 while ((status = sqlite3_step(statement)) ==
SQLITE_ROW)
   {
i++

if(i == 1)
  std::cout << "first row" << std::endl;

else std::cout << "another row" << std::endl;

num_bytes = sqlite3_column_bytes(statement, i);
num_bytes = num_bytes / sizeof(unsigned short);
image = new unsigned short[num_bytes];
image = (const unsigned
short*)sqlite3_column_blob(statement, i);

for(int i = 0; i < num_bytes; i++)
   std::cout << "image " << i << ": " << image[i]
<< std::endl;

//delete [] image; //causes a crash so far. 
   }

 status = sqlite3_finalize(statement);
 if (status != SQLITE_OK)
 std::cerr << "Error deleting prepared SQL
statement" << std::endl;

 else std::cout << "finalized statement successfully"
<< std::endl;

***

Output:

first row
image 0: 0
image 1: 3
image 2: 6
image 3: 9
image 4: 12
image 5: 15
image 6: 18
image 7: 21
image 8: 24
image 9: 27
another row
another row
finalized statement successfully

--so for some reason i get the first
blob(successfully, yes!) but it never increments after
that. the statement is also finalized successfully.
any reason why the other two blobs are not printing
out? 

as a side note i commented out the deleting of the
image because i got a major abort crash, no doubt
memory leaks/forbidden access issues. 

thanks!! 






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

> C S <[EMAIL PROTECTED]> wrote:
> >  thanks for the tip on the hex. now i can see my
> > entry of the blob. my original loop to fill the
> > dynamic array with unsigned shorts was this:
> >
> > imageArray = new unsigned short[10];
> >
> > for(int i = 0; i < 10; i++)
> >  imageArray[i] = i;
> >
> > so now if i run the program then do a:
> >
> > select imageID, hex(imageData) from Images;
> >
> > i get the result:
> >
> > 1|010002000300040005000600070008000900
> >
> > i am not sure where the first 2 leading zeros are
> > coming from
> 
> The first value (the one at index 0) in imageArray
> is zero, represented 
> by two zero bytes.
> 
> > now i need to be able to extract the data back out
> of
> > the blob with sqlite3_column_blob and
> > sqlite3_column_bytes so that i can repopulate an
> > unsigned short array.
> >
> > if i have multiple blobs in the table Images how
> would
> > i go about retrieving back all this information?
> 
> You prepare a select statement, e.g.
> 
> select imageID, imageData from Images;
> 
> Then you loop over all rows in the resultset with
> sqlite3_step call 
> (each call advances to the next row), and for each
> row call 
> sqlite3_column_* once for each column you are
> interested in.
> 
> 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
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
replying to my own message - 

update should my myString2 - that is not an actual
error, just copied and pasted it wrong. also i noticed
a problem with i in my final for loop. i changed it to
j since i was being incremented. i still get the same
output however. just to clarify. 

--- C S <[EMAIL PROTECTED]> wrote:

> Igor,
> 
>   thanks for all your help too man. here is what is
> in
> the table images so far when i do a select. its 3
> blobs:
> 
> sqlite> select imageID, hex(imageData) from Images;
> 1|00 0003 0006 0009 000C 000F 0012 0015 0018 001B 00
> 2|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
> 3|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
>
*
> 
> here is what i have now:
> 
> const unsigned short *image;
> int i = 0;
> 
> myString2 = "select imageID, imageData from Images";
> 
> int status = sqlite3_prepare_v2(db, update.c_str(),
> -1, , NULL);
> 
>  while ((status = sqlite3_step(statement)) ==
> SQLITE_ROW)
>{
> i++
> 
> if(i == 1)
>   std::cout << "first row" << std::endl;
> 
> else std::cout << "another row" << std::endl;
> 
> num_bytes = sqlite3_column_bytes(statement, i);
> num_bytes = num_bytes / sizeof(unsigned short);
> image = new unsigned short[num_bytes];
> image = (const unsigned
> short*)sqlite3_column_blob(statement, i);
> 
> for(int i = 0; i < num_bytes; i++)
>std::cout << "image " << i << ": " <<
> image[i]
> << std::endl;
> 
> //delete [] image; //causes a crash so far. 
>}
> 
>  status = sqlite3_finalize(statement);
>  if (status != SQLITE_OK)
>  std::cerr << "Error deleting prepared SQL
> statement" << std::endl;
> 
>  else std::cout << "finalized statement
> successfully"
> << std::endl;
> 
> ***
> 
> Output:
> 
> first row
> image 0: 0
> image 1: 3
> image 2: 6
> image 3: 9
> image 4: 12
> image 5: 15
> image 6: 18
> image 7: 21
> image 8: 24
> image 9: 27
> another row
> another row
> finalized statement successfully
> 
> --so for some reason i get the first
> blob(successfully, yes!) but it never increments
> after
> that. the statement is also finalized successfully.
> any reason why the other two blobs are not printing
> out? 
> 
> as a side note i commented out the deleting of the
> image because i got a major abort crash, no doubt
> memory leaks/forbidden access issues. 
> 
> thanks!! 
> 
> 
> 
> 
> 
> 
> --- Igor Tandetnik <[EMAIL PROTECTED]> wrote:
> 
> > C S <[EMAIL PROTECTED]> wrote:
> > >  thanks for the tip on the hex. now i can see my
> > > entry of the blob. my original loop to fill the
> > > dynamic array with unsigned shorts was this:
> > >
> > > imageArray = new unsigned short[10];
> > >
> > > for(int i = 0; i < 10; i++)
> > >  imageArray[i] = i;
> > >
> > > so now if i run the program then do a:
> > >
> > > select imageID, hex(imageData) from Images;
> > >
> > > i get the result:
> > >
> > > 1|010002000300040005000600070008000900
> > >
> > > i am not sure where the first 2 leading zeros
> are
> > > coming from
> > 
> > The first value (the one at index 0) in imageArray
> > is zero, represented 
> > by two zero bytes.
> > 
> > > now i need to be able to extract the data back
> out
> > of
> > > the blob with sqlite3_column_blob and
> > > sqlite3_column_bytes so that i can repopulate an
> > > unsigned short array.
> > >
> > > if i have multiple blobs in the table Images how
> > would
> > > i go about retrieving back all this information?
> > 
> > You prepare a select statement, e.g.
> > 
> > select imageID, imageData from Images;
> > 
> > Then you loop over all rows in the resultset with
> > sqlite3_step call 
> > (each call advances to the next row), and for each
> > row call 
> > sqlite3_column_* once for each column you are
> > interested in.
> > 
> > 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
> 



  

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] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
Igor,

  thanks for all your help too man. here is what is in
the table images so far when i do a select. its 3
blobs:

sqlite> select imageID, hex(imageData) from Images;
1|00 0003 0006 0009 000C 000F 0012 0015 0018 001B 00
2|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
3|00 0001 0002 0003 0004 0005 0006 0007 0008 0009 00
*

here is what i have now:

const unsigned short *image;
int i = 0;

myString2 = "select imageID, imageData from Images";

int status = sqlite3_prepare_v2(db, update.c_str(),
-1, , NULL);

 while ((status = sqlite3_step(statement)) ==
SQLITE_ROW)
   {
i++

if(i == 1)
  std::cout << "first row" << std::endl;

else std::cout << "another row" << std::endl;

num_bytes = sqlite3_column_bytes(statement, i);
num_bytes = num_bytes / sizeof(unsigned short);
image = new unsigned short[num_bytes];
image = (const unsigned
short*)sqlite3_column_blob(statement, i);

for(int i = 0; i < num_bytes; i++)
   std::cout << "image " << i << ": " << image[i]
<< std::endl;

//delete [] image; //causes a crash so far. 
   }

 status = sqlite3_finalize(statement);
 if (status != SQLITE_OK)
 std::cerr << "Error deleting prepared SQL
statement" << std::endl;

 else std::cout << "finalized statement successfully"
<< std::endl;

***

Output:

first row
image 0: 0
image 1: 3
image 2: 6
image 3: 9
image 4: 12
image 5: 15
image 6: 18
image 7: 21
image 8: 24
image 9: 27
another row
another row
finalized statement successfully

--so for some reason i get the first
blob(successfully, yes!) but it never increments after
that. the statement is also finalized successfully.
any reason why the other two blobs are not printing
out? 

as a side note i commented out the deleting of the
image because i got a major abort crash, no doubt
memory leaks/forbidden access issues. 

thanks!! 






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

> C S <[EMAIL PROTECTED]> wrote:
> >  thanks for the tip on the hex. now i can see my
> > entry of the blob. my original loop to fill the
> > dynamic array with unsigned shorts was this:
> >
> > imageArray = new unsigned short[10];
> >
> > for(int i = 0; i < 10; i++)
> >  imageArray[i] = i;
> >
> > so now if i run the program then do a:
> >
> > select imageID, hex(imageData) from Images;
> >
> > i get the result:
> >
> > 1|010002000300040005000600070008000900
> >
> > i am not sure where the first 2 leading zeros are
> > coming from
> 
> The first value (the one at index 0) in imageArray
> is zero, represented 
> by two zero bytes.
> 
> > now i need to be able to extract the data back out
> of
> > the blob with sqlite3_column_blob and
> > sqlite3_column_bytes so that i can repopulate an
> > unsigned short array.
> >
> > if i have multiple blobs in the table Images how
> would
> > i go about retrieving back all this information?
> 
> You prepare a select statement, e.g.
> 
> select imageID, imageData from Images;
> 
> Then you loop over all rows in the resultset with
> sqlite3_step call 
> (each call advances to the next row), and for each
> row call 
> sqlite3_column_* once for each column you are
> interested in.
> 
> 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] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Wilson, Ron P
spaces added for clarification:

1| 0100 0200 0300 0400 0500 0600 0700 0800 0900 

Ron Wilson, S/W Systems Engineer III, Tyco Electronics, 434.455.6453

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of C S
Sent: Wednesday, March 12, 2008 10:48 AM
To: General Discussion of SQLite Database
Subject: Re: [sqlite] SQL Blobs - what am i doing wrong here?(code
inside)

Dennis,
  thanks for the tip on the hex. now i can see my
entry of the blob. my original loop to fill the
dynamic array with unsigned shorts was this:

imageArray = new unsigned short[10];

for(int i = 0; i < 10; i++)
  imageArray[i] = i;

so now if i run the program then do a:

select imageID, hex(imageData) from Images;

i get the result:

1|010002000300040005000600070008000900

i am not sure where the first 2 leading zeros are
coming from but that looks to be ok. 

now i need to be able to extract the data back out of
the blob with sqlite3_column_blob and
sqlite3_column_bytes so that i can repopulate an
unsigned short array. 

if i have multiple blobs in the table Images how would
i go about retrieving back all this information? 

somehow though i would think that i would need the
number of blob entries in the database to make this
work. 

for(int i = 0; i < blob_entries; i++)
 {
  numBytes = sqlite3_column_bytes(statement, i);
  sqlite3_column_blob(statement, i);
 }

--what do you think? thanks all for the help. its
appreciated. 

--- Dennis Cote <[EMAIL PROTECTED]> wrote:

> C S wrote:
> > 
> > i get nothing. i have a printout statement to make
> > sure an imageID was created and it was
> successfully.
> > the array is indeed dynamic and has to be. to echo
> > this is what i have:
> > 
> > myString = "insert into Images(imageID, imageData)
> > values(?, ?);
> > 
> > status = sqlite3_prepare_v2(db, myString.c_str(),
> -1,
> > , NULL);
> > 
> > void *blob = reinterpretcast(imageArray);
> > 
> 
> This should be:
> 
> void *blob = reinterpretcast(imageArray);
> 
> > status = sqlite3_bind_blob(statement, 2, blob, 10
> *
> > sizeof(unsigned short), SQLITE_STATIC);
> > 
> > statusu = sqlite3_finalize(statement);
> > 
> 
> You need to execute the insert statement before you
> finalize it. You 
> have created and destroyed the statement, but have
> not executed it. Add 
> the following between the two statements above:
> 
> status = sqlite3_step(statement);
> if (status != SQLITE_OK) {
>  //process error
> }
> 
> 
> > 
> > however when i do:
> > 
> > select * from Images;
> > 
> 
> To dump the blob data in a human readable format you
> could use the hex() 
> SQL function. It will display each byte of the blob
> as two ASCII 
> characters that correspond to the hexadecimal value
> of the byte.
> 
> select imageID, hex(imageData) from Images;
> 
> HTH
> Dennis Cote
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 



 


Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

___
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] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Igor Tandetnik
C S <[EMAIL PROTECTED]> wrote:
>  thanks for the tip on the hex. now i can see my
> entry of the blob. my original loop to fill the
> dynamic array with unsigned shorts was this:
>
> imageArray = new unsigned short[10];
>
> for(int i = 0; i < 10; i++)
>  imageArray[i] = i;
>
> so now if i run the program then do a:
>
> select imageID, hex(imageData) from Images;
>
> i get the result:
>
> 1|010002000300040005000600070008000900
>
> i am not sure where the first 2 leading zeros are
> coming from

The first value (the one at index 0) in imageArray is zero, represented 
by two zero bytes.

> now i need to be able to extract the data back out of
> the blob with sqlite3_column_blob and
> sqlite3_column_bytes so that i can repopulate an
> unsigned short array.
>
> if i have multiple blobs in the table Images how would
> i go about retrieving back all this information?

You prepare a select statement, e.g.

select imageID, imageData from Images;

Then you loop over all rows in the resultset with sqlite3_step call 
(each call advances to the next row), and for each row call 
sqlite3_column_* once for each column you are interested in.

Igor Tandetnik 



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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
Dennis,
  thanks for the tip on the hex. now i can see my
entry of the blob. my original loop to fill the
dynamic array with unsigned shorts was this:

imageArray = new unsigned short[10];

for(int i = 0; i < 10; i++)
  imageArray[i] = i;

so now if i run the program then do a:

select imageID, hex(imageData) from Images;

i get the result:

1|010002000300040005000600070008000900

i am not sure where the first 2 leading zeros are
coming from but that looks to be ok. 

now i need to be able to extract the data back out of
the blob with sqlite3_column_blob and
sqlite3_column_bytes so that i can repopulate an
unsigned short array. 

if i have multiple blobs in the table Images how would
i go about retrieving back all this information? 

somehow though i would think that i would need the
number of blob entries in the database to make this
work. 

for(int i = 0; i < blob_entries; i++)
 {
  numBytes = sqlite3_column_bytes(statement, i);
  sqlite3_column_blob(statement, i);
 }

--what do you think? thanks all for the help. its
appreciated. 

--- Dennis Cote <[EMAIL PROTECTED]> wrote:

> C S wrote:
> > 
> > i get nothing. i have a printout statement to make
> > sure an imageID was created and it was
> successfully.
> > the array is indeed dynamic and has to be. to echo
> > this is what i have:
> > 
> > myString = "insert into Images(imageID, imageData)
> > values(?, ?);
> > 
> > status = sqlite3_prepare_v2(db, myString.c_str(),
> -1,
> > , NULL);
> > 
> > void *blob = reinterpretcast(imageArray);
> > 
> 
> This should be:
> 
> void *blob = reinterpretcast(imageArray);
> 
> > status = sqlite3_bind_blob(statement, 2, blob, 10
> *
> > sizeof(unsigned short), SQLITE_STATIC);
> > 
> > statusu = sqlite3_finalize(statement);
> > 
> 
> You need to execute the insert statement before you
> finalize it. You 
> have created and destroyed the statement, but have
> not executed it. Add 
> the following between the two statements above:
> 
> status = sqlite3_step(statement);
> if (status != SQLITE_OK) {
>  //process error
> }
> 
> 
> > 
> > however when i do:
> > 
> > select * from Images;
> > 
> 
> To dump the blob data in a human readable format you
> could use the hex() 
> SQL function. It will display each byte of the blob
> as two ASCII 
> characters that correspond to the hexadecimal value
> of the byte.
> 
> select imageID, hex(imageData) from Images;
> 
> HTH
> Dennis Cote
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 



  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
replying to my own message - i added the step function
that i needed...so:

sqlite_step(statement), in between the bind and
finalize..

i deleted all the entries from the table so i could
start over, so when i do a select on Images i get:

1|

so either somehow i cant see the contents of the blob
or there is nothing there at all.

any suggestions?

 
--- C S <[EMAIL PROTECTED]> wrote:

> Dennis mentioned before that it was not necessary to
> do a bind on something that was defined as an
> integer
> primary key. 
> 
> when i do:
> 
> select * from Images;
> 
> 1|
> 2|
> 
> thats pretty much it. there are two entries because
> i
> tried to pure void cast as the previous message
> suggest vs the char * way..and it "seems" to make no
> difference. 
> 
> if i am supposed to bind the imageID what would that
> bind look like? which bind statement am i looking
> for?
> thanks!!
> --- "Matthew L. Creech" <[EMAIL PROTECTED]> wrote:
> 
> > On Wed, Mar 12, 2008 at 9:37 AM, C S
> > <[EMAIL PROTECTED]> wrote:
> > >
> > >  myString = "insert into Images(imageID,
> > imageData)
> > >  values(?, ?);
> > >
> > >
> > >  status = sqlite3_prepare_v2(db,
> myString.c_str(),
> > -1,
> > >  , NULL);
> > >
> > >  void *blob = reinterpretcast *>(imageArray);
> > >
> > >
> > >  status = sqlite3_bind_blob(statement, 2, blob,
> 10
> > *
> > >  sizeof(unsigned short), SQLITE_STATIC);
> > >
> > >  statusu = sqlite3_finalize(statement);
> > >
> > >  return sqlite3_last_insert_row(db);
> > >
> > >  
> > >
> > >  however when i do:
> > >
> > >  select * from Images;
> > >
> > >  i get no results returned to me. i just get
> > returned
> > >  to the prompt. is there anything that i missed?
> > thanks
> > >  again!!!
> > >
> > 
> > You're missing a bind for the first column (the
> > imageID), and more
> > importantly, a call to sqlite3_step() - you do
> need
> > one, otherwise the
> > insert never executes.  :)
> > 
> > -- 
> > Matthew L. Creech
> > ___
> > sqlite-users mailing list
> > sqlite-users@sqlite.org
> >
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> > 
> 
> 
> 
>  
>

> Be a better friend, newshound, and 
> know-it-all with Yahoo! Mobile.  Try it now. 
>
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
> 
> 
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 



  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Dennis Cote
C S wrote:
> 
> i get nothing. i have a printout statement to make
> sure an imageID was created and it was successfully.
> the array is indeed dynamic and has to be. to echo
> this is what i have:
> 
> myString = "insert into Images(imageID, imageData)
> values(?, ?);
> 
> status = sqlite3_prepare_v2(db, myString.c_str(), -1,
> , NULL);
> 
> void *blob = reinterpretcast(imageArray);
> 

This should be:

void *blob = reinterpretcast(imageArray);

> status = sqlite3_bind_blob(statement, 2, blob, 10 *
> sizeof(unsigned short), SQLITE_STATIC);
> 
> statusu = sqlite3_finalize(statement);
> 

You need to execute the insert statement before you finalize it. You 
have created and destroyed the statement, but have not executed it. Add 
the following between the two statements above:

status = sqlite3_step(statement);
if (status != SQLITE_OK) {
 //process error
}


> 
> however when i do:
> 
> select * from Images;
> 

To dump the blob data in a human readable format you could use the hex() 
SQL function. It will display each byte of the blob as two ASCII 
characters that correspond to the hexadecimal value of the byte.

select imageID, hex(imageData) from Images;

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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Igor Tandetnik
C S <[EMAIL PROTECTED]> wrote:
> myString = "insert into Images(imageID, imageData)
> values(?, ?);
>
> status = sqlite3_prepare_v2(db, myString.c_str(), -1,
> , NULL);
>
> void *blob = reinterpretcast(imageArray);
>
> status = sqlite3_bind_blob(statement, 2, blob, 10 *
> sizeof(unsigned short), SQLITE_STATIC);
>
> statusu = sqlite3_finalize(statement);

You haven't actually _run_ the statement. This is done with sqlite3_step 
(between bind and finalize).

Igor Tandetnik



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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
Dennis mentioned before that it was not necessary to
do a bind on something that was defined as an integer
primary key. 

when i do:

select * from Images;

1|
2|

thats pretty much it. there are two entries because i
tried to pure void cast as the previous message
suggest vs the char * way..and it "seems" to make no
difference. 

if i am supposed to bind the imageID what would that
bind look like? which bind statement am i looking for?
thanks!!
--- "Matthew L. Creech" <[EMAIL PROTECTED]> wrote:

> On Wed, Mar 12, 2008 at 9:37 AM, C S
> <[EMAIL PROTECTED]> wrote:
> >
> >  myString = "insert into Images(imageID,
> imageData)
> >  values(?, ?);
> >
> >
> >  status = sqlite3_prepare_v2(db, myString.c_str(),
> -1,
> >  , NULL);
> >
> >  void *blob = reinterpretcast(imageArray);
> >
> >
> >  status = sqlite3_bind_blob(statement, 2, blob, 10
> *
> >  sizeof(unsigned short), SQLITE_STATIC);
> >
> >  statusu = sqlite3_finalize(statement);
> >
> >  return sqlite3_last_insert_row(db);
> >
> >  
> >
> >  however when i do:
> >
> >  select * from Images;
> >
> >  i get no results returned to me. i just get
> returned
> >  to the prompt. is there anything that i missed?
> thanks
> >  again!!!
> >
> 
> You're missing a bind for the first column (the
> imageID), and more
> importantly, a call to sqlite3_step() - you do need
> one, otherwise the
> insert never executes.  :)
> 
> -- 
> Matthew L. Creech
> ___
> sqlite-users mailing list
> sqlite-users@sqlite.org
>
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 



  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Matthew L. Creech
On Wed, Mar 12, 2008 at 9:37 AM, C S <[EMAIL PROTECTED]> wrote:
>
>  myString = "insert into Images(imageID, imageData)
>  values(?, ?);
>
>
>  status = sqlite3_prepare_v2(db, myString.c_str(), -1,
>  , NULL);
>
>  void *blob = reinterpretcast(imageArray);
>
>
>  status = sqlite3_bind_blob(statement, 2, blob, 10 *
>  sizeof(unsigned short), SQLITE_STATIC);
>
>  statusu = sqlite3_finalize(statement);
>
>  return sqlite3_last_insert_row(db);
>
>  
>
>  however when i do:
>
>  select * from Images;
>
>  i get no results returned to me. i just get returned
>  to the prompt. is there anything that i missed? thanks
>  again!!!
>

You're missing a bind for the first column (the imageID), and more
importantly, a call to sqlite3_step() - you do need one, otherwise the
insert never executes.  :)

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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread Wilson, Ron P
void *blob = reinterpretcast(imageArray);
void *blob = reinterpretcast(imageArray);

Ron Wilson, S/W Systems Engineer III, Tyco Electronics, 434.455.6453
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of C S
Sent: Wednesday, March 12, 2008 9:37 AM
To: General Discussion of SQLite Database
Subject: Re: [sqlite] SQL Blobs - what am i doing wrong here?(code
inside)

Dennis,

   thanks for your and everyone elses help with this
problem. i actually did everything you said here and
the program executed without any errors. 

i re-created the table to what you said as now so it
is blob now instead of text. 

i get nothing. i have a printout statement to make
sure an imageID was created and it was successfully.
the array is indeed dynamic and has to be. to echo
this is what i have:

myString = "insert into Images(imageID, imageData)
values(?, ?);

status = sqlite3_prepare_v2(db, myString.c_str(), -1,
, NULL);

void *blob = reinterpretcast(imageArray);

status = sqlite3_bind_blob(statement, 2, blob, 10 *
sizeof(unsigned short), SQLITE_STATIC);

statusu = sqlite3_finalize(statement);

return sqlite3_last_insert_row(db);



however when i do:

select * from Images;

i get no results returned to me. i just get returned
to the prompt. is there anything that i missed? thanks
again!!!


--- Dennis Cote <[EMAIL PROTECTED]> wrote:

> C S wrote:
> > hi all - i am trying once again to insert an
> image(an
> > array of shorts) into a blob. 
> > 
> > i created a table that is described as such:
> > 
> > CREATE TABLE Images{
> > imageID integer primary key,
> > imageData text not null);
> > 
> > #1 - is text ok here for a blob? maybe not - this
> may
> > be my entire problem. 
> > 
> 
> I would recommend using a column type of BLOB for
> blob data just to 
> avoid any confusion.
> 
> CREATE TABLE Images (
>  imageID integer primary key,
>  imageData blob not null);
> 
> 
> > anyway instead of using data in an image i just
> made
> > an array of unsigned shorts and filled it. a size
> of
> > 10. 
> > 
> > so here is what i am doing in the code. imageArray
> > holds the unsigned shorts(there are 10 of them)
> > 
> > char* blob = reinterpret_cast<char*>(imageArray);
> > 
> 
> Generally you should use a void* instead of a char*
> for blobs.
> 
> void* blob = reinterpret_cast<void*>(imageArray);
> 
> This pointer isn't needed if your imageArray is a
> true array of unsigned 
> shorts rather than a pointer to some dynamically
> allocated memory.
> 
> unsigned short imageArray[10] =
> {1,2,3,4,5,6,7,8,9,10};
> 
> 
> > string myString = "insert into Images(ImageID,
> > imageData) values(?, 'blob')";
> 
> Note, this 'blob' is a string literal and has no
> relation at all to the 
> blob variable you defined above. You need to use a ?
> for the second 
> parameter as well so that you can bind a value to it
> later.
> 
> string myString = "insert into Images(ImageID,
> imageData) values(?, ?)";
> 
> > 
> > //then i want to try to prepare the statement:
> > 
> > int status = sqlite3_prepare_v2(db,
> myString.c_str(),
> > -1, , NULL);
> > if( (status != SQLITE_OK) || (statement == NULL))
> >   cout << "Error preparing SQL Statement" << endl;
> > 
> 
> There is no need to check statement here. SQLite
> will return an error 
> code other than SQLITE_OK if it fails.
> 
> > 
> > //now i would like to bind the blob:
> > 
> > status = sqlite3_bind_blob(statement, 1,
> imageArray,
> > 10 * sizeof(unsigned short), SQLITE_TRANSIENT);
> > 
> 
> The blob will be the second parameter to the
> statement. This parameter 
> can be static as long as the image data will be
> stable until the 
> statement is executed by sqlite3_step().
> 
> If you want to use the blob pointer above you should
> do this:
> 
> status = sqlite3_bind_blob(statement, 2, blob,
>  10 * sizeof(unsigned short), SQLITE_STATIC);
> 
> If imageArray is a true array as shown above you can
> instead do this:
> 
> status = sqlite3_bind_blob(statement, 2, imageArray,
>  sizeof(imageArray), SQLITE_STATIC);
> 
> You have not bound a value to the first parameter,
> the imageId column, 
> so it will have a null value when the statement
> executes. This is OK 
> since the column is declared as "integer primary
> key" and SQLite will 
> assign a unique ID value.
> 
> > 
> > //execute statement for each row??
> > while( (status = sqlite3_step(statement)) ==
> > SQLITE_ROW);
&

Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-12 Thread C S
Dennis,

   thanks for your and everyone elses help with this
problem. i actually did everything you said here and
the program executed without any errors. 

i re-created the table to what you said as now so it
is blob now instead of text. 

i get nothing. i have a printout statement to make
sure an imageID was created and it was successfully.
the array is indeed dynamic and has to be. to echo
this is what i have:

myString = "insert into Images(imageID, imageData)
values(?, ?);

status = sqlite3_prepare_v2(db, myString.c_str(), -1,
, NULL);

void *blob = reinterpretcast(imageArray);

status = sqlite3_bind_blob(statement, 2, blob, 10 *
sizeof(unsigned short), SQLITE_STATIC);

statusu = sqlite3_finalize(statement);

return sqlite3_last_insert_row(db);



however when i do:

select * from Images;

i get no results returned to me. i just get returned
to the prompt. is there anything that i missed? thanks
again!!!


--- Dennis Cote <[EMAIL PROTECTED]> wrote:

> C S wrote:
> > hi all - i am trying once again to insert an
> image(an
> > array of shorts) into a blob. 
> > 
> > i created a table that is described as such:
> > 
> > CREATE TABLE Images{
> > imageID integer primary key,
> > imageData text not null);
> > 
> > #1 - is text ok here for a blob? maybe not - this
> may
> > be my entire problem. 
> > 
> 
> I would recommend using a column type of BLOB for
> blob data just to 
> avoid any confusion.
> 
> CREATE TABLE Images (
>  imageID integer primary key,
>  imageData blob not null);
> 
> 
> > anyway instead of using data in an image i just
> made
> > an array of unsigned shorts and filled it. a size
> of
> > 10. 
> > 
> > so here is what i am doing in the code. imageArray
> > holds the unsigned shorts(there are 10 of them)
> > 
> > char* blob = reinterpret_cast(imageArray);
> > 
> 
> Generally you should use a void* instead of a char*
> for blobs.
> 
> void* blob = reinterpret_cast(imageArray);
> 
> This pointer isn't needed if your imageArray is a
> true array of unsigned 
> shorts rather than a pointer to some dynamically
> allocated memory.
> 
> unsigned short imageArray[10] =
> {1,2,3,4,5,6,7,8,9,10};
> 
> 
> > string myString = "insert into Images(ImageID,
> > imageData) values(?, 'blob')";
> 
> Note, this 'blob' is a string literal and has no
> relation at all to the 
> blob variable you defined above. You need to use a ?
> for the second 
> parameter as well so that you can bind a value to it
> later.
> 
> string myString = "insert into Images(ImageID,
> imageData) values(?, ?)";
> 
> > 
> > //then i want to try to prepare the statement:
> > 
> > int status = sqlite3_prepare_v2(db,
> myString.c_str(),
> > -1, , NULL);
> > if( (status != SQLITE_OK) || (statement == NULL))
> >   cout << "Error preparing SQL Statement" << endl;
> > 
> 
> There is no need to check statement here. SQLite
> will return an error 
> code other than SQLITE_OK if it fails.
> 
> > 
> > //now i would like to bind the blob:
> > 
> > status = sqlite3_bind_blob(statement, 1,
> imageArray,
> > 10 * sizeof(unsigned short), SQLITE_TRANSIENT);
> > 
> 
> The blob will be the second parameter to the
> statement. This parameter 
> can be static as long as the image data will be
> stable until the 
> statement is executed by sqlite3_step().
> 
> If you want to use the blob pointer above you should
> do this:
> 
> status = sqlite3_bind_blob(statement, 2, blob,
>  10 * sizeof(unsigned short), SQLITE_STATIC);
> 
> If imageArray is a true array as shown above you can
> instead do this:
> 
> status = sqlite3_bind_blob(statement, 2, imageArray,
>  sizeof(imageArray), SQLITE_STATIC);
> 
> You have not bound a value to the first parameter,
> the imageId column, 
> so it will have a null value when the statement
> executes. This is OK 
> since the column is declared as "integer primary
> key" and SQLite will 
> assign a unique ID value.
> 
> > 
> > //execute statement for each row??
> > while( (status = sqlite3_step(statement)) ==
> > SQLITE_ROW);
> > 
> 
> There is no need for a while loop here. An insert
> statement can only 
> step once, and sqlite3_step() will return
> SQLITE_DONE or some other 
> error code.
> 
> > //free the prepared statement
> > status = sqlite3_finalize(statement);
> > if(status != SQLITE_OK)
> >   cout << "Error deleting prepared SQL statement"
> <<
> > endl;
> >
>
=
> > 
> > i actually get the last status check output,
> saying
> > that there was an error deleting the statement.
> when i
> > looked that up it says there was a problem with
> the
> > prepare statement being successful or nothing
> happens
> > at all when clearly i did and clearly the status
> was
> > ok too since i didnt get an error message there. 
> > 
> 
> I'm not sure why the finalize would fail, unless
> perhaps your prepare 
> also failed, and you didn't have a valid statement
> pointer to pass to 
> sqlite3_finalize().
> 
> > can anyone 

Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-11 Thread Dennis Cote
C S wrote:
> hi all - i am trying once again to insert an image(an
> array of shorts) into a blob. 
> 
> i created a table that is described as such:
> 
> CREATE TABLE Images{
> imageID integer primary key,
> imageData text not null);
> 
> #1 - is text ok here for a blob? maybe not - this may
> be my entire problem. 
> 

I would recommend using a column type of BLOB for blob data just to 
avoid any confusion.

CREATE TABLE Images (
 imageID integer primary key,
 imageData blob not null);


> anyway instead of using data in an image i just made
> an array of unsigned shorts and filled it. a size of
> 10. 
> 
> so here is what i am doing in the code. imageArray
> holds the unsigned shorts(there are 10 of them)
> 
> char* blob = reinterpret_cast(imageArray);
> 

Generally you should use a void* instead of a char* for blobs.

void* blob = reinterpret_cast(imageArray);

This pointer isn't needed if your imageArray is a true array of unsigned 
shorts rather than a pointer to some dynamically allocated memory.

unsigned short imageArray[10] = {1,2,3,4,5,6,7,8,9,10};


> string myString = "insert into Images(ImageID,
> imageData) values(?, 'blob')";

Note, this 'blob' is a string literal and has no relation at all to the 
blob variable you defined above. You need to use a ? for the second 
parameter as well so that you can bind a value to it later.

string myString = "insert into Images(ImageID, imageData) values(?, ?)";

> 
> //then i want to try to prepare the statement:
> 
> int status = sqlite3_prepare_v2(db, myString.c_str(),
> -1, , NULL);
> if( (status != SQLITE_OK) || (statement == NULL))
>   cout << "Error preparing SQL Statement" << endl;
> 

There is no need to check statement here. SQLite will return an error 
code other than SQLITE_OK if it fails.

> 
> //now i would like to bind the blob:
> 
> status = sqlite3_bind_blob(statement, 1, imageArray,
> 10 * sizeof(unsigned short), SQLITE_TRANSIENT);
> 

The blob will be the second parameter to the statement. This parameter 
can be static as long as the image data will be stable until the 
statement is executed by sqlite3_step().

If you want to use the blob pointer above you should do this:

status = sqlite3_bind_blob(statement, 2, blob,
 10 * sizeof(unsigned short), SQLITE_STATIC);

If imageArray is a true array as shown above you can instead do this:

status = sqlite3_bind_blob(statement, 2, imageArray,
 sizeof(imageArray), SQLITE_STATIC);

You have not bound a value to the first parameter, the imageId column, 
so it will have a null value when the statement executes. This is OK 
since the column is declared as "integer primary key" and SQLite will 
assign a unique ID value.

> 
> //execute statement for each row??
> while( (status = sqlite3_step(statement)) ==
> SQLITE_ROW);
> 

There is no need for a while loop here. An insert statement can only 
step once, and sqlite3_step() will return SQLITE_DONE or some other 
error code.

> //free the prepared statement
> status = sqlite3_finalize(statement);
> if(status != SQLITE_OK)
>   cout << "Error deleting prepared SQL statement" <<
> endl;
> =
> 
> i actually get the last status check output, saying
> that there was an error deleting the statement. when i
> looked that up it says there was a problem with the
> prepare statement being successful or nothing happens
> at all when clearly i did and clearly the status was
> ok too since i didnt get an error message there. 
> 

I'm not sure why the finalize would fail, unless perhaps your prepare 
also failed, and you didn't have a valid statement pointer to pass to 
sqlite3_finalize().

> can anyone help me out as to what might be going on
> here? thanks so much in advance 
> 

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


Re: [sqlite] SQL Blobs - what am i doing wrong here?(code inside)

2008-03-11 Thread Igor Tandetnik
C S <[EMAIL PROTECTED]> wrote:
> so here is what i am doing in the code. imageArray
> holds the unsigned shorts(there are 10 of them)
>
> char* blob = reinterpret_cast(imageArray);
>
> string myString = "insert into Images(ImageID,
> imageData) values(?, 'blob')";

This statement inserts some value (to be bound later) into ImageID 
column, and a string literal 'blob' into imageData column. Is that 
really what you want? From your problem description, I expected 
something like

insert into Images(imageData) values(?);

> int status = sqlite3_prepare_v2(db, myString.c_str(),
> -1, , NULL);
>
> status = sqlite3_bind_blob(statement, 1, imageArray,
> 10 * sizeof(unsigned short), SQLITE_TRANSIENT);

You are binding the blob to a parameter that the statement will try to 
place into ImageID column. You cannot insert a BLOB into a field 
declared INTEGER PRIMARY KEY.

> //execute statement for each row??

How do you mean? You are running an INSERT statement, not a SELECT 
statement. For each row of what?

> while( (status = sqlite3_step(statement)) ==
> SQLITE_ROW);

Luckily, sqlite3_step will never return SQLITE_ROW when executing INSERT 
(or UPDATE or DELETE) statement.

Igor Tandetnik



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