前端之家收集整理的这篇文章主要介绍了
Sqlite3简单应用,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
sqlite3 *db;
sqlite3_stmt *stat;
char *zErrMsg = 0;
char buffer2[1024]="0";
int result = sqlite3_open16(_T("testh.db"),&db);;
if(result == sqlITE_OK)
{
AfxMessageBox(_T("success1"));
}
else
{
AfxMessageBox(_T("fail1"));
}
sqlite3_exec(db,"CREATE TABLE list1 (fliename varchar(128) UNIQUE,fzip blob,ntest int,ntest1 int);",&zErrMsg);
if (zErrMsg)
{
AfxMessageBox(_T("fail2"));
sqlite3_free(zErrMsg);
}
//sqlite3_prepare 第一个参数是sqlite3 * 指针;
//第二个参数是sql 字符串,可以包含多个sql语句;
//第三个参数是sql 字符串长度,-1 sqlite 自己计算;
//第四个参数是sqlite3_stmt ** 类型返回的是sql 解析的语句;
//第个参数是const char *pzTail 用于指向zsql中下个sql语句开始的地址上,所以pzTail并没有重新开辟内存空间;
const char *pzTail =NULL;
const char *zsql = "insert into list1 values ('2.bmp',?,1); insert into list1 values ('2.bmp',1);";
int nRet = sqlite3_prepare(db,zsql,-1,&stat,&pzTail);
FILE *fp;
long filesize = 0;
char * ffile;
fp = fopen("2.bmp","rb");
if(fp != NULL)
{
fseek(fp,SEEK_END);
filesize = ftell(fp);
fseek(fp,SEEK_SET);
ffile = new char[filesize+1];
size_t sz = fread(ffile,sizeof(char),filesize+1,fp);
fclose(fp);
}
//sqlite3_bind_xxx 函数第一个参数是sqlite3_prepare 后的sqlite3_stmt 指针变量;
//第二个参数是列的索引(从索引0开始);
sqlite3_bind_blob(stat,1,ffile,filesize,NULL);
delete(ffile);
sqlite3_bind_int(stat,2,100);
sqlite3_step(stat);
//每次调用sqlite3_prepare 函数sqlite 会重新开辟sqlite3_stmt空间,;
//所以在使用同一个sqlite3_stmt 指针再次调用sqlite3_prepare 前;
//需要调用sqlite3_finalize先释放空间;
sqlite3_finalize(stat);
//--------------------------------------
sqlite3_prepare(db,"select * from list1;",0);
//在调用sqlite3_column_count获取查询结果列信息的时候不需要先执行sqlite3_step就可以获取;
int nColNum = sqlite3_column_count(stat);
int r = sqlite3_step(stat);
//在调用sqlite3_column_type 和sqlite3_column_name获取列类型和列名称信息前需要先执行sqlite3_step;
/*;
#define sqlITE_INTEGER 1
#define sqlITE_FLOAT 2
#define sqlITE_TEXT 3
#define sqlITE_BLOB 4
#define sqlITE_NULL 5
*/
int colType = sqlite3_column_type(stat,0);
const char *pchColName = sqlite3_column_name(stat,0);
colType = sqlite3_column_type(stat,1);
pchColName = sqlite3_column_name(stat,1);
colType = sqlite3_column_type(stat,2);
pchColName = sqlite3_column_name(stat,2);
colType = sqlite3_column_type(stat,3);
pchColName = sqlite3_column_name(stat,3);
while(r == sqlITE_ROW)
{
const void * test = sqlite3_column_blob(stat,1);
int size = sqlite3_column_bytes(stat,1);
sprintf(buffer2,"%s",test);
FILE *fp2;
fp2 = fopen("outfile.bmp","wb");
if(fp2 != NULL)
{
size_t ret = fwrite(test,size,fp2);
fclose(fp2);
}
const unsigned char * ptest = sqlite3_column_text(stat,0);
int ntest = sqlite3_column_int(stat,2);
int ntest1 = sqlite3_column_int(stat,3);
r = sqlite3_step(stat);
}
sqlite3_finalize(stat);
sqlite3_close(db);
原文链接:https://www.f2er.com/sqlite/200889.html