SQLITE Blob写入错误 [英] SQLITE Blob writing error

查看:120
本文介绍了SQLITE Blob写入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一段代码,用于在SQLITE中编写和读取blob

数据库。

我从blob结构中读取一个名为''点的问题''

任何帮助将不胜感激


Reagards

格雷格


# 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;

}积分;


int

main(int argc,char ** argv)

{

char * errMsg = 0;

int rc;

sqlite3_stmt * stmt = NULL;

积分*积分;

长numPoints;

长尺寸;

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,无法打开数据库:%s \ n,sqlite3_errmsg(db1));

退出(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)");

//写记录


numPoints = 5;


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

pointsP = (积分*)malloc(大小);


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, 插入punkty值(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); < br $>
}


sqlite3_step(stmt);


rc = sqlite3_finalize(stmt);

if(rc)

{

fprintf(stderr," ERORR:%s \ n",sqlite3_errmsg(db1));

退出(1);

}


if(pointsP-> x!= NULL)

{

免费( pointsP-> x);

pointsP-> x = NULL;

}


免费(pointsP);

pointsP = NULL;

//读取记录

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); //也许在这里

是一个错误

}


for(iii = 0; iii< num_punkt; iii ++)

{

printf(" x =%。2f \ nn",pointsP-> x [iii]); //这里打印垃圾

}


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


}

sqlite3_finalize(stmt);

}

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);
}

推荐答案

11月14日,5:41 * am,向导 < g ... @ wp.plwrote:
On Nov 14, 5:41*am, "wizard" <g...@wp.plwrote:

下面是一段代码,用于在SQLITE中编写和读取blob

数据库。

我从blob结构中读取一个名为''积分'的问题'

任何帮助将不胜感激


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错误:%s \ n",errMsg);

* }


}


typedef struct points

{

long numPoints;

double * x;


}积分;


int

main(int argc,char ** argv)

{

* char * errMsg = 0;

* int rc;

* sqlite3_stmt * stmt = NULL;

* Points ** pointsP;

*长numPoints;

*长尺寸;

* int iii;

*字符文本[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,无法打开数据库:%s \ n,sqlite3_errmsg(db1));

* *退出(1);

*}

* exec_simple(" PRAGMA page_size = 8192");

* exec_simple(" ; PRAGMA同步s = OFF");

* exec_simple(" PRAGMA count_changes = OFF");

* exec_simple(" PRAGMA temp_store = MEMORY");


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


//写记录


* numPoints = 5;


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

* pointsP =(Points *)malloc(size);
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);



不要施放malloc!我不明白计算尺寸的b $ b。为什么乘以一?为什么要添加sizeof(int)?


如果你想要pointsP {能够有numPoints积分,

只需使用:

pointsP = malloc(numPoints * sizeof(* pointsP));

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));
>
* memcpy( pointsP, &numPoints, sizeof(int));



我不知道你为什么要这样做,但这可能是错的。

numPoints很长。为什么你只复制它的一部分?

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));
>
* nRecordSize = 4;

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



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

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;

*}
>
*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)
(snip)



-

Fred Kleinschmidt

--
Fred Kleinschmidt


Fred< fr ***************** @ boeing.comwrites:
Fred <fr*****************@boeing.comwrites:

11月14日,5:41?* am,向导 < g ... @ wp.plwrote:
On Nov 14, 5:41?*am, "wizard" <g...@wp.plwrote:



< snip>

<snip>


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



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


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



你可能打算输入:


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


-

Ben。

You probably meant to type:

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

--
Ben.


" wizard" < gg ** @ wp.plwrites:
"wizard" <gg**@wp.plwrites:

下面是一段代码,用于在SQLITE

数据库中编写和读取blob。
Here is a piece of code below for writing and reading blobs in SQLITE
database.



这个界面的细节在这里是偏离主题的,但我认为

你的第一个问题与C和内存有关分配。


< snip>

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 ;

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



< snip>

<snip>


numPoints = 5;


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

pointsP =(Points *)malloc(size);
numPoints=5;

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



这都错了。首先,我将乘法乘以1和

不必要的演员。然后我将不得不问你想要的数据是什么?
写入这个blob。如果你真的需要一个双*,那么

你需要为这个结构分配空间+也许一些填充*房间

5个双打。测试程序中的一种方法是:


struct blob {

积分p;

double actual_points [5] ;

};


并分配其中一个:


struct blob * bp = malloc(sizeof * bp);

bp-> p.numPoints = 5;

bp-> px = bp-> actual_points;


当然,你很可能在Points结构中想要双打
,在这种情况下,typedef是错误的你和

应该搜索struct hack。

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));
memcpy( pointsP, &numPoints, sizeof(int));



哇! sizeof(int)并不总是sizeof(long)。为什么不:


pointsP-> numPoints = numPoints;



Woah! sizeof(int) is not always sizeof(long). Why not:

pointsP->numPoints = numPoints;

?


nRecordSize = 4;


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

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



执行两次分配后,我认为您无法将

点数据转换为单个blob。现在,这可能是这样的,但是我怀疑它是怎么回事。

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;
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;
}

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



这都错了。我不能纠正它,因为我不知道是什么

" shape"您真正想要制作的数据。你首先需要告诉我们


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。

<snip>

--
Ben.


这篇关于SQLITE Blob写入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆