开发工具:
文件大小: 529kb
下载次数: 0
上传时间: 2019-06-30
详细说明:
## 使用SQLite创建数据库
**sqlite3_open(const char \*filename, sqlite3 \**ppDb)**
如果 *filename* 参数是 NULL 或 ':memory:',那么 sqlite3_open() 将会在 RAM 中创建一个内存数据库,这只会在 session 的有效时间内持续。
如果文件名 filename 不为 NULL,那么 sqlite3_open() 将使用这个参数值尝试打开数据库文件。如果该名称的文件不存在,sqlite3_open() 将创建一个新的命名为该名称的数据库文件并打开。
简单的来说就是会打开一个数据库文rc
QTTE OK )I
45
fprintf(stderr, SQL error: s\n", ZErrMsg)
46
sqlite3 free(ZErrMsg)
47
Else(
48
fprintf(s-dout, "Tablc crcatcd cuccessfully\
1i=e3 close(db)
gca
3 Opened database successfully
4 Table created successfully
数据库内筒如下:
数据库结构浏览数据但)编辑杂注执行5Q
表: COMPANY
新建
D
NAME
AGE
ADDRESS
SALARY
Filter
Filter
Filter
Filter
Filter
在上述数据表中插入数据
2 #include
3+incluce
5 static int callback(void *NotUsed, int argc, char **argv, char
t l
7
for(i=0; isargci i++)i
tf(
ColName [i]
rgu
pr
14 int main(int arg
15
16
li=e3 *do
int rci
char xsg
****★*★*★*★****第一步,打开数据库文件,获取数据库文件操作句柄
& ab
f(
fprintf(a=derr, Can't cpcn databasc: s\n", sclitc3 crrmsg(db)
xit(o)
26
Else
fprintf(s=derr, Opened database successfully\n)i
*次次***次******第二步、定义数据操作语句****x*****x***x**/
31
sq1="工 NSERT工 NTO COMPANY(工D,NM三,AGE, ADDRESS, SALARY)"
VATUES (1,Paul
,20000.00)
33
WINSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY)\
34
"VLUs(2,"A11en',25;' Texas',15000.00);"
TNSERT TNTO COMPANY (TD, NAME,, AGF, ADDRESS, SATARY)\
"VALJ=S(3,"cddy',23," Norway',20000.00);"
"工 NSERT工 NTO COMPANY(工D,NEM三,AGE, ADDRESS, SALARY)"
"VATs(4,'Mark',25,"Ricn-Mond',65000.00);";
39
40
大大大大大大大大大大大大大大大大大大大大
第三步、执行建表语句建立数据表大大x大大太大大大下大大太大
41
rc- sqlite3 exec(db, sql, callback, 0, &7FrrMsg)i
42
SQ⊥⊥上K)「
43
fprintf (s=derr, "SQL error: sn", zerrmsg)i
44
e⊥ae
fprintf(stdout, "Records created successfully\n")
48
sqi-e3c⊥oae(db);
49
return o
19.
2 Opened database successfully
3 Records created successfully
数据库结构(浏览数据编辑杂注()执行500
表:回 COMPANY
D
NAME
AGE
ADDRESS
SALARY
Filter
Filter
Filter
Filter
Filter
Paul
32
California.0
22
Allen
25
Texas
15000.0
33
Teddy
23
Norway
20000.0
Mark
25
Rich-Mond 65000.0
数据的获取
获取数据中需要用到回调函数的使用
1 typedef int (*sqlite3 callback)(
yod*,/*sq1ite3 exec()中第四个参数+/
ntr
*行中的列数*/
4chax**,/*行中的字符串
5
列名的字符串数组
1#includ
3 incluce
5 static int callback(void *data, int argc, char **argv, char x*azCoIName)i
int
7
fprintf (stderr, s:,(const charx) data)
for(i=0; i
tatic int callback(void *data, int argc, char x*argv, char **azcoIName )i
6
int
fprintf(stdcrr, 05:",(const charx) data)
for(i=0; icargc i++)
printf("s=s\n", az. ColName [i], argv[i] argv[il :"NUTT,")i
prin=f("\n")
12
return O
13
14
15 int main(int argc, char* argv[])
1
char * zErrMsg
Int rci
char sgli
cons- charx data =callback function called"
/ Open database *
24
rc- sqlite.3 open("test. db", &db)i
26
fprintf (s=derr, Can't open database: os\n", sclite3 errmsg(db))
exit(o)i
fprintf(a=derr, "Opened databasc successfully\n")
30
3
d ol stat
sql ="UPDATE COMPANY set SALARY =25000.00 where ID=li
W SFT.ECT from COMPANYi
35
36
Execute SCL statement */
rc- sqlite3 exec(db, sql, callback, (void*)data, &7.FrrMsg)i
38
if(yc!= SOLITE○F)
39
fprintf(s-derr, "SQL error: s\n", ZErrMsg)
40
glite3 free (z. FrrMsg)
Clack
fprintf(s-dout, "Operation done success fully\n")i
sq1-e3c⊥oae(db);
45
return o
46}
执行结果
1 s gcc creat db updat
I sqlite3
s ./aout
pened database successfull
4 Callback function called: ID-1
6 AGE =32
ADDRESS=Ca⊥ formia
8 SALARY=25000.0#原来是2000
10
allbacs function called: Id=2
11 NAME Allen
13 ADDRESS Texas
14 SALARY=15000.0
16 Callback function called: ID =3
17 NAME= Teddy
18AGE=23
19 ADDRESS -Norway
20 SALARY=20000.0
21
22 Callback function called: ID=4
23 NAME= Mark
24AGE=25
25 ADDRESS =Rich-Mond
26 SALARY=65000.0
8 Operation done successfully
delate操作
1 #incluce
2 incluce
3#incluce
5 static int callback(void xdata, int argc, char xxargv, char **azcolName)i
nt
fprintf(stder-,
(const chark) data)i
argci i++)
printf("s =s\n", azcolName [i], argv[i] argv[i] :"NULL")
prin=f("\n")
1111
15 int main(int argc, chark argv[I
16
sqli-e3 xdb
char zerrMsg =0i
int rci
char sali
cons- chark data =callbac< function called'
/x Open database */
24
open ("test. db
db
25
if( rc)t
fprintf(a=derr, "Cant open databasc: s\n", sclitc3 crrmsg(db))i
it(0);
Else
fprintf(scderr, "Opened database successfully\n")i
32
/ Crcatc merged SQL statement
33
sqI=DELETE from COMPANY where ID=2i
34
U SELECT from CoMPaNy il
35
/ Execute SCL statement x/
可
) data, &zErrMsg
38
if(rc!=SQ⊥⊥EK)「
f
ZErrMsg)
40
sqlite3 free (ZerrMsg)i
Eclac
42
fprintf (s=dout, Or
cessfully\n")
43
gli=c3 close(db)
执行结果
1$./aout
2 Cpened database successfully
function called: T
4 NAMF= Paul
5GE=32
6 ADDRESS california
7 SALIARY=25000.0
9 Callback function called
12 ADDRESS
Norway
SALARY = 20000.0
15 Callback function called: ID=4
6 NAMF:=Mark
17AGE=25
18 ADDRESS=Rich-Mond
19 SALARY=65000.0
20
21 Cperation done successfully
22 andrewandrew-Thurley: /work/linux-sys/SQT,ite/scs
口口
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.