By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,784 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,784 IT Pros & Developers. It's quick & easy.

SQLITE Blob writing error

P: n/a
Here is a piece of code below for writing and reading blobs in SQLITE
database.
I ' ve a problem to read from blob struct called 'Points'
Any help would be appreciated

Reagards
Greg

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "sqlite3.h"

typedef unsigned char uchar;

char dbname[1000];
static sqlite3 *db1;

void
exec_simple (char *stm)
{
int rc;
char *errMsg = 0;

rc = sqlite3_exec (db1, stm, NULL, 0, &errMsg);
if (rc != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n", errMsg);
}

}

typedef struct points
{
long numPoints;
double *x;
}Points;

int
main (int argc, char **argv)
{
char *errMsg = 0;
int rc;
sqlite3_stmt *stmt = NULL;
Points *pointsP;
long numPoints;
long size;
int iii;
char text[32];
uchar *pabyRec;
int nRecordSize;
int num_punkt;
int nOffset;

strcpy(dbname,".\\test.db");

unlink (dbname);

rc = sqlite3_open (dbname, &db1);

if (rc)
{
fprintf (stderr, "Can't open database: %s\n", sqlite3_errmsg (db1));
exit (1);
}

exec_simple ("PRAGMA page_size=8192");
exec_simple ("PRAGMA synchronous = OFF");
exec_simple ("PRAGMA count_changes = OFF");
exec_simple ("PRAGMA temp_store = MEMORY");
exec_simple ("CREATE TABLE punkty(id INTEGER PRIMARY KEY, value BLOB)");
// write record

numPoints=5;

size = numPoints * 1 * sizeof(double) + sizeof(int);
pointsP = (Points *) malloc(size);

memcpy( pointsP, &numPoints, sizeof(int));

nRecordSize = 4;

pointsP->x = (double *) calloc(numPoints, sizeof(double));

for( iii = 0; iii < numPoints; iii++ )
{
pointsP->x[iii] = (double)iii*100+0.01;
}

for( iii = 0; iii < numPoints; iii++ )
{
memcpy( pointsP + nRecordSize, pointsP->x + iii, sizeof(double));
nRecordSize += 1 * 8;
}

rc = sqlite3_prepare(db1, "insert into punkty values (1,?);", -1, &stmt,
NULL);

if (rc)
{
fprintf (stderr, "ERORR: %s\n", sqlite3_errmsg (db1));
exit (1);
}

rc =sqlite3_bind_blob(stmt, 1, pointsP, size, NULL);
if (rc)
{
fprintf (stderr, "ERORR: %s\n", sqlite3_errmsg (db1));
exit (1);
}

sqlite3_step(stmt);

rc =sqlite3_finalize(stmt);
if (rc)
{
fprintf (stderr, "ERORR: %s\n", sqlite3_errmsg (db1));
exit (1);
}

if( pointsP->x != NULL )
{
free( pointsP->x );
pointsP->x=NULL;
}

free(pointsP);
pointsP=NULL;
// read record
rc = sqlite3_prepare (db1, "select *from punkty", -1, &stmt, NULL);

if (rc != SQLITE_OK)
{
fprintf (stderr, " %s\n", sqlite3_errmsg (db1));
}

while (sqlite3_step (stmt) == SQLITE_ROW)
{

if (rc != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n", errMsg);
}

size = sqlite3_column_bytes(stmt, 1);

pointsP= (Points *)malloc(size);

memcpy(pointsP,sqlite3_column_blob(stmt, 1),size);
printf("size=%ld\n",size);

memcpy( &num_punkt, pointsP, 4 );

printf("pointsP->numPoints=%ld\n",num_punkt);

nOffset = 4;

pointsP->x = (double *) calloc(num_punkt, sizeof(double));

for( iii = 0; iii < num_punkt; iii++ )
{
memcpy(pointsP->x + iii, pointsP + nOffset + iii * 8, 8); //maybe here
is a fault
}

for(iii=0;iii<num_punkt;iii++)
{
printf("x=%.2f\n",pointsP->x[iii]); // here prints rubbish
}

printf("ID=%ld\n", sqlite3_column_int(stmt,0));

}
sqlite3_finalize (stmt);
}
Nov 14 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Nov 14, 5:41*am, "wizard" <g...@wp.plwrote:
Here is a piece of code below for writing and reading blobs in SQLITE
database.
I ' ve a problem to read from blob struct called 'Points'
Any help would be appreciated

Reagards
Greg

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "sqlite3.h"

typedef unsigned char uchar;

char dbname[1000];
static sqlite3 *db1;

void
exec_simple (char *stm)
{
* int rc;
* char *errMsg = 0;

* rc = sqlite3_exec (db1, stm, NULL, 0, &errMsg);
* if (rc != SQLITE_OK)
* {
* * fprintf (stderr, "SQL error: %s\n", errMsg);
* }

}

typedef struct points
{
long numPoints;
double *x;

}Points;

int
main (int argc, char **argv)
{
* char *errMsg = 0;
* int rc;
* sqlite3_stmt *stmt = NULL;
* Points **pointsP;
* long numPoints;
* long size;
* int iii;
* char text[32];
* * uchar *pabyRec;
* * int * * * * nRecordSize;
int num_punkt;
int nOffset;

* strcpy(dbname,".\\test.db");

* unlink (dbname);

* rc = sqlite3_open (dbname, &db1);

* if (rc)
* {
* * fprintf (stderr, "Can't open database: %s\n", sqlite3_errmsg (db1));
* * exit (1);
* }

* exec_simple ("PRAGMA page_size=8192");
* exec_simple ("PRAGMA synchronous = OFF");
* exec_simple ("PRAGMA count_changes = OFF");
* exec_simple ("PRAGMA temp_store = MEMORY");

* exec_simple ("CREATE TABLE punkty(id INTEGER PRIMARY KEY, value BLOB)");

// write record

* numPoints=5;

* size = numPoints * 1 * sizeof(double) + sizeof(int);
* pointsP = (Points *) malloc(size);
Don't cast malloc! And I don't understand the computation
of size. Why multiply by one? why add sizeof(int)?

If you want pointsP{ to be able to have numPoints Points,
just use:
pointsP = malloc( numPoints * sizeof(*pointsP) );

>
* memcpy( pointsP, &numPoints, sizeof(int));
I don't know why you are doing this, but it is probably wrong.
numPoints is a long. why are you copying only part of it?
>
* nRecordSize = 4;

* * pointsP->x = (double *) calloc(numPoints, sizeof(double));
pointsP->x = calloc(numPoints, sizeof(*points)->x) );
>
*for( iii = 0; iii < numPoints; iii++ )
* * {
* pointsP->x[iii] = (double)iii*100+0.01;
*}

*for( iii = 0; iii < numPoints; iii++ )
*{
* * memcpy( pointsP + nRecordSize, pointsP->x + iii, sizeof(double));
* * nRecordSize += 1 * 8;
*}
Now you are copying a double into the middle of a long,
probably clobbering something. What is it you are trying to do?
(snip)
--
Fred Kleinschmidt
Nov 14 '08 #2

P: n/a
Fred <fr*****************@boeing.comwrites:
On Nov 14, 5:41*am, "wizard" <g...@wp.plwrote:
<snip>
>* * pointsP->x = (double *) calloc(numPoints, sizeof(double));

pointsP->x = calloc(numPoints, sizeof(*points)->x) );
You probably meant to type:

pointsP->x = calloc(numPoints, sizeof(*pointsP->x)) );

--
Ben.
Nov 14 '08 #3

P: n/a
"wizard" <gg**@wp.plwrites:
Here is a piece of code below for writing and reading blobs in SQLITE
database.
The details of that interface would be off-topic here, but I think
your first problems are just to do with C and memory allocation.

<snip>
typedef struct points
{
long numPoints;
double *x;
}Points;
<snip>
numPoints=5;

size = numPoints * 1 * sizeof(double) + sizeof(int);
pointsP = (Points *) malloc(size);
This is all wrong. First, I'd remove the multiplication by 1 and the
unnecessary cast. Then I'd have to ask what data you are trying to
write into this "blob". If you really need a double * in there, then
you need to allocate space for this struct + maybe some padding * room
for 5 doubles. One way to do that in the test program is:

struct blob {
Points p;
double actual_points[5];
};

and to allocate one of these:

struct blob *bp = malloc(sizeof *bp);
bp->p.numPoints = 5;
bp->p.x = bp->actual_points;

Of course, it is just as likely that you really wanted the doubles to
in the Points struct, in which case the typedef is wrong and you
should search for the "struct hack".
memcpy( pointsP, &numPoints, sizeof(int));
Woah! sizeof(int) is not always sizeof(long). Why not:

pointsP->numPoints = numPoints;

?
nRecordSize = 4;

pointsP->x = (double *) calloc(numPoints, sizeof(double));
After doing two allocations, I think you will not be able to get the
points data into a single "blob". Now, it could be that this is how
SQLite3 works, but I doubt it.
for( iii = 0; iii < numPoints; iii++ )
{
pointsP->x[iii] = (double)iii*100+0.01;
The cast is unnecessary.
}

for( iii = 0; iii < numPoints; iii++ )
{
memcpy( pointsP + nRecordSize, pointsP->x + iii, sizeof(double));
nRecordSize += 1 * 8;
and this is all wrong. I can't correct it because I don't know what
"shape" of data you are really trying to make. You need to tell us
that first of all.
}
<snip>

--
Ben.
Nov 14 '08 #4

P: n/a
"wizard" <gg**@wp.plwrites:
Here is a piece of code below for writing and reading blobs in SQLITE
database.
I ' ve a problem to read from blob struct called 'Points'
Any help would be appreciated
[176 lines of code deleted]

Some indication of *what* problem you're having would be extremely
helpful.

I haven't (yet) taken the time to look at your code, but if the
problem is directly related to the SQLITE stuff, then this isn't the
place to ask about it. If the SQLITE stuff is incidental, and your
real problem has to do with C, then you're in the right place -- but
without some more information it's going to be difficult to help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 14 '08 #5

P: n/a
In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:
>You probably meant to type:
pointsP->x = calloc(numPoints, sizeof(*pointsP->x)) );
The parentheses do not match.

*You* probably meant to type:

pointsP->x = calloc(numPoints, sizeof *pointsP->x); /* :-) */

--
Ike
Nov 14 '08 #6

P: n/a
ik*@localhost.claranet.nl (Ike Naar) writes:
In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:
>>You probably meant to type:
pointsP->x = calloc(numPoints, sizeof(*pointsP->x)) );

The parentheses do not match.

*You* probably meant to type:

pointsP->x = calloc(numPoints, sizeof *pointsP->x); /* :-) */
Good catch but, no. I /meant/ to type

pointsP->x = calloc(numPoints, sizeof(*pointsP->x));

Though I prefer sizeof without brackets (as in your correction) I
intended to follow the style I was trying to correct. All of Fred's
examples use bracketed sizeof. In an attempt to get fidelity I
cut-and-pasted Fred's without spotting its extra ).

--
Ben.
Nov 14 '08 #7

P: n/a
Thanks for all replies.
Sorry I forgot what I'm exactly going to do.
My case is very simple for You (So I mean)
Now I'm clearing detaily.
I would like to write/read geometry of simple 2D objects (shapes but with
holes) to Sqlite blob field.
I delivered a little test snippet - what I,m doing wrong.

Maybe my data structure as below is wrong
typedef struct points
{
long numPoints;
double *x;
}Points;

Maybe somebody could give some correct piece of code.

Pls help me.
Greg
Nov 17 '08 #8

P: n/a
On 17 Nov, 06:43, "wizard" <g...@wp.plwrote:
Thanks for all replies.
Sorry I forgot what I'm exactly going to do.
My case is very simple for You (So I mean)
Now I'm clearing detaily.
I would like to write/read geometry of simple 2D objects (shapes but with
holes) to Sqlite blob field.
I delivered a little test snippet - what I,m doing wrong.

Maybe my data structure as below is wrong
typedef struct points
{
long numPoints;
double *x;

}Points;

Maybe somebody could give some correct piece of code.
you need to post on a news group where sqlite is on-topic

--
Nick Keighley

Nov 17 '08 #9

P: n/a
you need to post on a news group where sqlite is on-topic

I didn't found any SQLITE group on net
Would You be so kind to give me any sqlite target news group?

Greg
Nov 17 '08 #10

P: n/a
On 17 Nov, 08:31, "wizard" <g...@wp.plwrote:
you need to post on a news group where sqlite is on-topic

I didn't found any SQLITE group on net
Would You be so kind to give me any sqlite target news group?
this is really your problem not mine.
But 10s on google found me their home page on they appear
to have mailing lists. Try subscribing to one of those
and see if they can help

http://www.sqlite.org/support.html

--
Nick Keighley
Nov 17 '08 #11

P: n/a
"wizard" <gg**@wp.plwrites:
Thanks for all replies.
Sorry I forgot what I'm exactly going to do.
My case is very simple for You (So I mean)
Now I'm clearing detaily.
I would like to write/read geometry of simple 2D objects (shapes but with
holes) to Sqlite blob field.
I delivered a little test snippet - what I,m doing wrong.

Maybe my data structure as below is wrong
typedef struct points
{
long numPoints;
double *x;
}Points;

Maybe somebody could give some correct piece of code.
There is a standard C part to this question. A "blob" is common term
used to refer to a chuck of uninterpreted data. This means that the
struct you have looks inappropriate; it requires a count and a pointer
to a separate array that contains the data. Now you can do some
tricks to make this into a single, contiguous block of memory but it
is simpler to array in the structure.

If you can something close to a C99 compiler, the language a mechanism
for this purpose: you leave the size empty in the declaration and
allocate the extra space using malloc:

struct points {
long int num;
double data[];
};

struct points blob_ptr;
blob_ptr = malloc(sizeof *blob_ptr + 2 * N * sizeof *blob_ptr->data);

(I multiplied by 2 because I assume you need 2 doubles for each
point.)

If you can't use the device, you should search for the "struck hack".
In older C you need to have an array of size 1, and adjust things
accordingly.

--
Ben.
Nov 17 '08 #12

P: n/a

Uzytkownik "Ben Bacarisse" <be********@bsb.me.uknapisal w wiadomosci
news:87************@bsb.me.uk...
"wizard" <gg**@wp.plwrites:
>Thanks for all replies.
Sorry I forgot what I'm exactly going to do.
My case is very simple for You (So I mean)
Now I'm clearing detaily.
I would like to write/read geometry of simple 2D objects (shapes but with
holes) to Sqlite blob field.
I delivered a little test snippet - what I,m doing wrong.

Maybe my data structure as below is wrong
typedef struct points
{
long numPoints;
double *x;
}Points;

Maybe somebody could give some correct piece of code.

There is a standard C part to this question. A "blob" is common term
used to refer to a chuck of uninterpreted data. This means that the
struct you have looks inappropriate; it requires a count and a pointer
to a separate array that contains the data. Now you can do some
tricks to make this into a single, contiguous block of memory but it
is simpler to array in the structure.

If you can something close to a C99 compiler, the language a mechanism
for this purpose: you leave the size empty in the declaration and
allocate the extra space using malloc:

struct points {
long int num;
double data[];
};

struct points blob_ptr;
blob_ptr = malloc(sizeof *blob_ptr + 2 * N * sizeof *blob_ptr->data);

(I multiplied by 2 because I assume you need 2 doubles for each
point.)

If you can't use the device, you should search for the "struck hack".
In older C you need to have an array of size 1, and adjust things
accordingly.

--
Ben.
Thanks a lot Ben
I lost my hope somebody help me.
Thanks a lot for Your reply
This is clear for me.
But how a can declare a string to store diffrent lenght strings (e.g
"100/15", "1", "1111155668989" etc.)
not to loose memory (not static text) I tried as above but a can't compile
(under VC 6.0)
- variable text

struct points {
long int num;
double data[];
char text[];
};

Greg

Nov 18 '08 #13

P: n/a
On 17 Nov, 16:06, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
If you can't use the device, you should search for the "struck hack".
In older C you need to have an array of size 1, and adjust things
accordingly.
that's "struct hack".
I don't normally correct typos, but that *was* a search string...
Nov 18 '08 #14

P: n/a
Nick Keighley <ni******************@hotmail.comwrites:
On 17 Nov, 16:06, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>If you can't use the device, you should search for the "struck hack".
In older C you need to have an array of size 1, and adjust things
accordingly.

that's "struct hack".
I don't normally correct typos, but that *was* a search string...
Absolutely. That's the last place there should be one. Ta.

--
Ben.
Nov 18 '08 #15

P: n/a
"wizard" <gg**@wp.plwrites:
Uzytkownik "Ben Bacarisse" <be********@bsb.me.uknapisal w wiadomosci
news:87************@bsb.me.uk...
>"wizard" <gg**@wp.plwrites:
<snip>
>>I would like to write/read geometry of simple 2D objects (shapes but with
holes) to Sqlite blob field.
I delivered a little test snippet - what I,m doing wrong.

Maybe my data structure as below is wrong
typedef struct points
{
long numPoints;
double *x;
}Points;
<snip>
> struct points {
long int num;
double data[];
};

struct points blob_ptr;
blob_ptr = malloc(sizeof *blob_ptr + 2 * N * sizeof *blob_ptr->data);

(I multiplied by 2 because I assume you need 2 doubles for each
point.)

If you can't use the device, you should search for the "struck hack".
In older C you need to have an array of size 1, and adjust things
accordingly.

--
Ben.
Best not to quote sig blocks even though mine is short. In fact it is
best to cut out the parts that you are not commenting on but leaving
enough for context.

<snip>
But how a can declare a string to store diffrent lenght strings (e.g
"100/15", "1", "1111155668989" etc.)
not to loose memory (not static text) I tried as above but a can't compile
(under VC 6.0)
- variable text

struct points {
long int num;
double data[];
char text[];
};
In VC I don't you can use the [] syntax (I know little about VC but it
is a C99 construct and VC is C90). Even if you could, you can't have
more than one such array in a struct and it has to be the last member.
It is probably better to re-think how you are string the data in this
database. DBs are good at storing text, so I don't see why you've now
added a char array to your "blob".

--
Ben.
Nov 18 '08 #16

P: n/a
wizard wrote:
....
struct points {
long int num;
double data[];
char text[];
};
A struct can only have one flexible array member, and that member must
be the last member of the struct.
Nov 18 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.