前端之家收集整理的这篇文章主要介绍了
SQLite内存数据库简单使用范例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "sqlite3.h"
#pragma comment(lib,"sqlite3.lib")
#include <windows.h>
#include <iostream>
#include "CodingConv.h"
int main(int argc,char* argv[])
{
int rc,i,ncols;
sqlite3 *db;
sqlite3_stmt *stmt;
char *sql;
const char *tail;
char* zErrMsg;
rc=sqlite3_open(":memory:",&db);
rc=sqlite3_exec(db,"attach 'c:\\mydatabase.db' as new_Db",&zErrMsg);
sqlite3_exec(db,"detach 'C:\\mydatabase.db",&zErrMsg);
//char temp[256]="c:\\数据库\\mydatabase.db";
//char FileRoot[256];
//CCodingConv::GB2312_2_UTF8(FileRoot,256,temp,0);
//
//rc=sqlite3_open(FileRoot,&db);
//rc = sqlite3_open("mydatabase.db",&db);
if(rc!= sqlITE_OK )
{
printf("Failed to open database!\n");
return -1;
}
sql="select * from test";
rc=sqlite3_prepare(db,sql,(int)strlen(sql),&stmt,&tail);
if(rc!=sqlITE_OK)
{
fprintf(stderr,"sql error:%sn",sqlite3_errmsg(db));
}
rc=sqlite3_step(stmt);
ncols=sqlite3_column_count(stmt);
while(rc==sqlITE_ROW)
{
for(i=0;i<ncols;i++)
{
fprintf(stderr,"%s",sqlite3_column_text(stmt,i));
}
fprintf(stderr,"\n");
rc=sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
printf("\n");
return 0;
}