Ⅰ 请教如何把sqlite嵌入到我的c程序里去
示例代码:
// name: query.c
// This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !
// Author : zieckey All rights reserved.
// data : 2006/11/18
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#define _DEBUG_
int main( void )
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if( rc )
{
fprintf(stderr, "Can't open database: %s
", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else printf("You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^
");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
char *sql = " CREATE TABLE SensorData(
ID INTEGER PRIMARY KEY,
SensorID INTEGER,
SiteNum INTEGER,
Time VARCHAR(12),
SensorParameter REAL
);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
#ifdef _DEBUG_
printf("zErrMsg = %s
", zErrMsg);
#endif
//插入数据
sql = "INSERT INTO "SensorData" VALUES(NULL , 1 , 1 , '200605011206', 18.9 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sql = "INSERT INTO "SensorData" VALUES(NULL , 1 , 1 , '200605011306', 16.4 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
int nrow = 0, ncolumn = 0;
char **azResult; //二维数组存放结果
//查询数据
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
int i = 0 ;
printf( "row:%d column=%d
" , nrow , ncolumn );
printf( "
The result of querying is :
" );
Ⅱ sqlite3 怎么查询数据库中所有的表 用C语言实现
SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
在C语言中用这个查询语句
Ⅲ sqlite3中用c语言写数据库的时候,假如我建立一个学生信息库时候,在现查询功能时,如果没有这个
select * from yourtabel where xuehao = yournum
不存在 会返回空
Ⅳ c语言实现sqlite3设计学生信息管理系统
#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"stdlib.h"
voidlook();/*声明查看函数*/
voidsave();/*声明保存函数*/
voidsearch_name();/*声明按姓名查看函数*/
voidsearch_number();/*声明按学号查看函数*/
voidorder();/*声明排序函数*/
voiddel();/*声明删除函数*/
intopenl();/*声明打开函数*/
voidwelcome();/*声明我的个人信息函数*/
voidtype();/*声明输入函数*/
voidsee();/*声明打开并查看文件函数*/
structstudent/*定义学生信息的结构体类型*/
{
charnum[15];/*学号*/
Ⅳ 怎么用C语言动态往sqlite3里面插入数据
首先说明这个问题困扰了我很长时间了,严格地说应该有两天,不过终于通过sqlite的官方文档解决了。
For example, assume the string variable zText contains text as follows:
char *zText = "It's a happy day!";
One can use this text in an SQL statement as follows:
char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText); sqlite3_exec(db, zSQL, 0, 0, 0); sqlite3_free(zSQL);
摘自liudong123的专栏
Ⅵ SQLITE数据库C语言API 想使当数据库不存在时 sqlite3_open 不创建数据库
改用sqlite3_open_v2函数打开
int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags,const char *zVfs);
前两个参数与sqllite3_open一样,flags设置为SQLITE_OPEN_READWRITE,如果数据库不存在就不创建,只返回一个error
参数zVfs允许应用程序命名一个虚拟文件系统(Virtual File System)模块,用来与数据库连接。VFS作为SQlite library和底层存储系统(如某个文件系统)之间的一个抽象层,通常客户应用程序可以简单的给该参数传递一个NULL指针,以使用默认的VFS模块。
Ⅶ 怎样用C语言访问Sqlite数据库
SQLITE 官网就有例子、文档,,,,,,,SQLITE本身就是C写的
Ⅷ C语言如何调用SQLite3中的接口
下载 sqlite3 库,放在项目文件夹中,包含 sqlite3.h 头文件,包含 sqlite3.lib 导入库,将 sqlite3.dll 复制到 .exe 所在目录。
代码如下:
#include<stdio.h>
#include<stdlib.h>
//包含sqlite3头文件
#include"../sqlite-3.24.0/include/sqlite3.h"
//添加sqlite3lib库
#pragmacomment(lib,"../sqlite-3.24.0/x86/sqlite3.lib")
inttable_exist_callback(void*data,intargc,char**argv,char**azColName)
{
intval=atoi(argv[0]);
if(val==0){
return-1;
}
return0;
}
intprint_callback(void*data,intargc,char**argv,char**azColName)
{
for(inti=0;i<argc;i++){
printf("%s=%s ",azColName[i],argv[i]);
}
printf(" ");
return0;
}
intmain()
{
sqlite3*db=NULL;
char*sql=NULL;
char*errMsg=NULL;
//打开数据库
intret=sqlite3_open("data.db",&db);
if(ret!=SQLITE_OK){
printf("打开数据失败,%s ",sqlite3_errmsg(db));
exit(-1);
}
//判断students是否存在,如果不存在,则创建表students
sql="selectcount(*)fromsqlite_masterwheretype='table'andname='students';";
ret=sqlite3_exec(db,sql,table_exist_callback,NULL,NULL);
if(ret!=SQLITE_OK){
//创建students表
sql="createtablestudents(numbervarchar(10),namevarchar(20),ageinteger);";
ret=sqlite3_exec(db,sql,NULL,NULL,&errMsg);
if(ret!=SQLITE_OK){
printf("创建表失败,%s. ",errMsg);
sqlite3_close(db);
sqlite3_free(errMsg);
exit(-1);
}
}
//插入数据到students表
sql="insertintostudents(number,name,age)values('100001','Barry',23);"
"insertintostudents(number,name,age)values('100002','Oliver',33);";
ret=sqlite3_exec(db,sql,NULL,NULL,&errMsg);
if(ret!=SQLITE_OK){
printf("插入表数据失败,%s. ",errMsg);
sqlite3_close(db);
sqlite3_free(errMsg);
exit(-1);
}
//查询students表数据
sql="select*fromstudents";
ret=sqlite3_exec(db,sql,print_callback,NULL,&errMsg);
if(ret!=SQLITE_OK){
printf("查询数据失败,%s. ",errMsg);
sqlite3_close(db);
sqlite3_free(errMsg);
exit(-1);
}
//关闭数据库
sqlite3_close(db);
system("pause");
return0;
}
项目源码:网页链接
Ⅸ 用C语言做个sqlite数据库~
). 打开VC新建一个“Win32 Dynamic-Link Library”工程,命名为:sqlite32). 在接下来的对话框中选择"An empty DLL project",点 FINISH->OK3). 将源码中所有的 *.c *.h *.def 复制到工程文件夹下4). 在工程的Source File中添加你下载到的SQLite源文件中所有*.c文件,注意这里不要添加shell.c和tclsqlite.c这两个文件。5). 将 SQLite 源文件中的 sqlite3.def 文件添加到在工程的Source File中6). 在Header File中添加你下载到的SQLite源文件中所有*.h文件,7). 开始编译,Build(F7)一下也许到这里会遇到一个错误:e:\zieckey\sqlite\sqlite3\sqlite3ext.h(22) : fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory经检查发现,源码中包含sqlite3.h都是以 #include <sqlite3.h> 方式包含的,这就是说编译器在系统默认路径中搜索,这样当然搜索不到 sqlite3.h 这个头文件啦,这时可以改为 #include "sqlite3.h" ,让编译器在工程路径中搜索,但是如果还有其他地方也是以 #include <sqlite3.h> 方式包含的,那么改源码就显得有点麻烦,好了,我们可以这样,在菜单栏依次选择:Tools->Options...->Directeries在下面的Directeries选项中输入你的 sqlite3.h 的路径,这里也就是你的工程目录.添加好后,我们在编译一下就好了,最后我们在工程目录的 Debug 目录生成了下面两个重要文件:动态链接库文件 sqlite3.dll 和引入库文件 sqlite3.lib二. 使用动态链接库下面我们来编写个程序来测试下我们的动态链接库.在VC下新建一个空的"Win32 Console Application" Win32控制台程序,工程命名为:TestSqliteOnWindows再新建一个 test.cpp 的C++语言源程序,源代码如下:// name: test.cpp// This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !// Author : zieckey// data : 2006/11/28#include <stdio.h>#include <stdlib.h>#include "sqlite3.h" #define _DEBUG_int main( void ){sqlite3 *db=NULL;char *zErrMsg = 0;int rc;rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件if( rc ){fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));sqlite3_close(db);return (1);}else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
Ⅹ 在C语言下,如何备份一个sqlite3数据库,如何使用备份命令操作能给一段代码最好,谢谢!
sqlite3数据库就是一个文件,将该文件拷贝一份即完成备份。