473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

SQLITE Blob writing error

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
16 6634
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
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
"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
"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
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
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
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
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
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
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
"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

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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Greg Miller | last post by:
I have an application that uses sqlite3 to store job/error data. When I log in as a German user the error codes generated are translated into German. The error code text is then stored in the db....
3
by: bolly | last post by:
Hi, I've been putting Python data into a sqlite3 database as tuples but when I retrieve them they come back as unicode data e.g 'u(1,2,3,4)'.How can I change it back to a tuple so I can use it as...
10
by: Luigi | last post by:
Hello all! I'm a newbie in PHP. I have written a short script that tries to update a SQLite database with the user data. It is pretty simple, something like this: <?php $sqlite =...
8
by: Gilles Ganault | last post by:
Hello I need to compile PHP5 with SQLite statically and without PDO. I don't need DB-independence, so it's fine using sqlite_*() functions instead of PDO. 1. I've downloaded, compiled, and...
3
by: Daniel Fetchinson | last post by:
Does Python 2.5.2's embedded SQLite support full text searching? Sqlite itself is not distributed with python. Only a python db api compliant wrapper is part of the python stdlib and as such it...
0
by: Joe Goldthwaite | last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would automatically come with some version of sqlite. The fact that it doesn't is what was causing me to get the strange...
1
by: Harris Kosmidhs | last post by:
Hello. I started writing an application and used PDO to connect to an sqlite file.The sqlite DB was created with the Sqllite extension of firefox. Now I want to switch to CakePHP. I used the...
3
by: Jeff | last post by:
I've been writing all my utilities using PDO, and MySQL. So, I'm working on a virtual server and I'm getting a missing driver error. DSN look like this: $DSN='mysql:dbname=... PHP info does...
20
by: timotoole | last post by:
Hi all, On a (sun) webserver that I use, there is python 2.5.1 installed. I'd like to use sqlite3 with this, however sqlite3 is not installed on the webserver. If I were able to compile sqlite...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.