sql3学习.docx
- 文档编号:7403976
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:13
- 大小:18.10KB
sql3学习.docx
《sql3学习.docx》由会员分享,可在线阅读,更多相关《sql3学习.docx(13页珍藏版)》请在冰豆网上搜索。
sql3学习
linux c编程操作数据库(sqlite3应用)(
C/C++语言中调用sqlite的函数接口来实现对数据库的管理(创建数据库、创建表格、插入数据、查询、数据、删除数据等)。
首先要编译好
sqlite的库文件:
libsqlite3.a libsqlite3.la libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6 pkgconfig
可执行文件:
sqlite3
本次测试:
sqlite3的库文件目录是:
/usr/local/lib
可执行文件sqlite3的目录是:
/usr/local/bin
头文件sqlite3.h的目录是:
/usr/local/include
用ls命令查看如下:
[root@localhostconfig]#ls/usr/local/lib
libclamav.a libclamunrar_iface.a libclamunrar.so libsqlite3.so
libclamav.la libclamunrar_iface.la libclamunrar.so.5 libsqlite3.so.0
libclamav.so libclamunrar_iface.so libclamunrar.so.5.0.3 libsqlite3.so.0.8.6
libclamav.so.5 libclamunrar_iface.so.5 libmstring.so pkgconfig
libclamav.so.5.0.3 libclamunrar_iface.so.5.0.3 libsqlite3.a
libclamunrar.a libclamunrar.la libsqlite3.la
此目录下包含库文件:
libsqlite3.a libsqlite3.la libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6 pkgconfig
开始sqlite编程:
1.下面是一个C程序的例子,显示怎么使用sqlite的C/C++接口.数据库的名字由第一个参数取得且第个参数或更多的参数是SQL执行语句.
这个函数调用sqlite3_open()打开数据库,并且调用sqlite3_close()关闭数据库连接。
程序一:
opendbslite.c:
viewplaincopytoclipboardprint?
#include
#include
#include
intmain(void)
{
sqlite3*db=NULL;
char*zErrMsg=0;
intrc;
//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
rc=sqlite3_open("zieckey.db",&db);
if(rc)
{
fprintf(stderr,"Can'topendatabase:
%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
exit
(1);
}
elseprintf("Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
\nCongratulations!
Havefun!
^-^\n");
sqlite3_close(db);//关闭数据库
return0;
}
#include
#include
#include
intmain(void)
{
sqlite3*db=NULL;
char*zErrMsg=0;
intrc;
//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
rc=sqlite3_open("zieckey.db",&db);
if(rc)
{
fprintf(stderr,"Can'topendatabase:
%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
exit
(1);
}
elseprintf("Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
\nCongratulations!
Havefun!
^-^\n");
sqlite3_close(db);//关闭数据库
return0;
}
编译(问题):
[root@localhostliuxltest]#gcc-oopendbsqliteopendbsqlite.c
/tmp/ccuquUQN.o:
Infunction`main':
opendbsqlite.c:
(.text+0x2e):
undefinedreferenceto`sqlite3_open'
opendbsqlite.c:
(.text+0x42):
undefinedreferenceto`sqlite3_errmsg'
opendbsqlite.c:
(.text+0x67):
undefinedreferenceto`sqlite3_close'
opendbsqlite.c:
(.text+0x8a):
undefinedreferenceto`sqlite3_close'
collect2:
ldreturned1exitstatus
编译(解决):
出现上述问题是因为没有找到库文件的问题。
由于用到了用户自己的库文件,所用应该指明所用到的库,我们可以这样编译:
[root@localhostliuxltest]#gcc-oopendbsqliteopendbsqlite.c-lsqlite3
用-lsqlite3选项就可以了(前面我们生成的库文件是libsqlite3.so.0.8.6等,去掉前面的lib和后面的版本标志,就剩下sqlite3了所以是-lsqlite3)
执行:
[root@localhostliuxltest]#./opendbsqlite
Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
Congratulations!
Havefun!
^-^
[root@localhostliuxltest]#
2.插入:
insert
在C语言中向数据库插入数据:
sqlite3_exec的函数原型说明如下:
intsqlite3_exec(
sqlite3*,
constchar*sql,
sqlite_callback,
void*,
char**errmsg
);
程序二:
insert.c:
viewplaincopytoclipboardprint?
#include
#include
#include"sqlite3.h"
#define_DEBUG_
intmain(void)
{
sqlite3*db=NULL;
char*zErrMsg=0;
intrc;
rc=sqlite3_open("zieckey.db",&db);//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if(rc)
{
fprintf(stderr,"Can'topendatabase:
%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
exit
(1);
}
elseprintf("Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
\nCongratulations!
Havefun!
^-^\n");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在zErrMsg中
char*sql="CREATETABLESensorData( \
IDINTEGERPRIMARYKEY, \
SensorIDINTEGER, \
SiteNumINTEGER, \
TimeVARCHAR(12), \
SensorParameterREAL \
);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
#ifdef_DEBUG_
printf("%s\n",zErrMsg);
#endif
//插入数据
char*sql1="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011206',18.9);";
sqlite3_exec(db,sql1,0,0,&zErrMsg);
char*sql2="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011306',16.4);";
sqlite3_exec(db,sql2,0,0,&zErrMsg);
sqlite3_close(db);//关闭数据库
return0;
}
#include
#include
#include"sqlite3.h"
#define_DEBUG_
intmain(void)
{
sqlite3*db=NULL;
char*zErrMsg=0;
intrc;
rc=sqlite3_open("zieckey.db",&db);//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if(rc)
{
fprintf(stderr,"Can'topendatabase:
%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
exit
(1);
}
elseprintf("Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
\nCongratulations!
Havefun!
^-^\n");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在zErrMsg中
char*sql="CREATETABLESensorData( \
IDINTEGERPRIMARYKEY, \
SensorIDINTEGER, \
SiteNumINTEGER, \
TimeVARCHAR(12), \
SensorParameterREAL \
);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
#ifdef_DEBUG_
printf("%s\n",zErrMsg);
#endif
//插入数据
char*sql1="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011206',18.9);";
sqlite3_exec(db,sql1,0,0,&zErrMsg);
char*sql2="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011306',16.4);";
sqlite3_exec(db,sql2,0,0,&zErrMsg);
sqlite3_close(db);//关闭数据库
return0;
}
编译运行:
[root@localhostliuxltest]#gcc-oinsertinsert.c-lsqlite3
[root@localhostliuxltest]#./insert
Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
Congratulations!
Havefun!
^-^
(null)
(null)
(null)
[root@localhostliuxltest]#
查看是否插入数据:
[root@localhostliuxltest]#/usr/local/bin/sqlite3zieckey.db"select*fromSensorData"
3.查询:
SELETE
C语言中查询数据库中的数据。
函数接口sqlite3_get_table(db,sql,&azResult,&nrow,&ncolumn,&zErrMsg);
解释:
intsqlite3_get_table(sqlite3*,constchar*sql,char***result,int*nrow,int*ncolumn,char**errmsg);
result中是以数组的形式存放你所查询的数据,首先是表名,再是数据。
nrow,ncolumn分别为查询语句返回的结果集的行数,列数,没有查到结果时返回0
程序三:
query.c:
viewplaincopytoclipboardprint?
#include
#include
#include"sqlite3.h"
#define_DEBUG_
intmain(void)
{
sqlite3*db=NULL;
char*zErrMsg=0;
intrc;
rc=sqlite3_open("zieckey.db",&db);//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if(rc)
{
fprintf(stderr,"Can'topendatabase:
%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
exit
(1);
}
elseprintf("Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
\nCongratulations!
Havefun!
^-^\n");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在zErrMsg中
char*sql="CREATETABLESensorData( \
IDINTEGERPRIMARYKEY, \
SensorIDINTEGER, \
SiteNumINTEGER, \
TimeVARCHAR(12), \
SensorParameterREAL \
);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
#ifdef_DEBUG_
printf("zErrMsg=%s\n",zErrMsg);
#endif
//插入数据
sql="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011206',18.9);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
sql="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011306',16.4);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
intnrow=0,ncolumn=0;
char**azResult;//二维数组存放结果
//查询数据
sql="SELECT*FROMSensorData";
sqlite3_get_table(db,sql,&azResult,&nrow,&ncolumn,&zErrMsg);
inti=0;
printf("row:
%dcolumn=%d\n",nrow,ncolumn);
printf("\nTheresultofqueryingis:
\n");
for(i=0;i<(nrow+1)*ncolumn;i++)
printf("azResult[%d]=%s\n",i,azResult[i]);
//释放掉 azResult的内存空间
sqlite3_free_table(azResult);
#ifdef_DEBUG_
printf("zErrMsg=%s\n",zErrMsg);
#endif
sqlite3_close(db);//关闭数据库
return0;
}
#include
#include
#include"sqlite3.h"
#define_DEBUG_
intmain(void)
{
sqlite3*db=NULL;
char*zErrMsg=0;
intrc;
rc=sqlite3_open("zieckey.db",&db);//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if(rc)
{
fprintf(stderr,"Can'topendatabase:
%s\n",sqlite3_errmsg(db));
sqlite3_close(db);
exit
(1);
}
elseprintf("Youhaveopenedasqlite3databasenamedzieckey.dbsuccessfully!
\nCongratulations!
Havefun!
^-^\n");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在zErrMsg中
char*sql="CREATETABLESensorData( \
IDINTEGERPRIMARYKEY, \
SensorIDINTEGER, \
SiteNumINTEGER, \
TimeVARCHAR(12), \
SensorParameterREAL \
);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
#ifdef_DEBUG_
printf("zErrMsg=%s\n",zErrMsg);
#endif
//插入数据
sql="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011206',18.9);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
sql="INSERTINTO\"SensorData\"VALUES(NULL,1,1,'200605011306',16.4);";
sqlite3_exec(db,sql,0,0,&zErrMsg);
intnrow=0,ncolumn=0;
char**azResult;//二维数组存放结果
//查询数据
sql="SELECT*FROMSensorData";
sqlite3_get_table(db,sql,&azResult,&nrow,&ncolumn,&zErrMsg);
inti=0;
printf("row:
%dcolumn=%d\n",nrow,ncolumn);
printf("\nTheresultofqueryingis:
\n");
for(i=0;i<(nrow+1)*ncolumn;i++)
printf("azResult[%d]=%s\n",i,azResult[i]
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- sql3 学习