赞
踩
Linux下选择合适的目录下执行:sudo apt-get install sqlite3
然后输入密码等待。
验证是否安装成功:进入数据库
sqlite3
退出(quit前面有一个点)
.quit
数据库里面,获取帮助
.help
创建表stu,包含id,name,score属性
create table stu(id Integer, name Char, score Integer);
添加一条记录(向表里面加入一条信息)
insert into stu values(1001,'zhangsan', 99);
查询所有记录(查询表里面的信息)
select *from stu;
插入部分字段(向表里面添加不完整的信息,比如没有id号)
insert into stu(name,score) values('lisi', 59);
查询部分字段(比如不查询id号)
select name,score from stu;
根据属性查询(比如想查询stu表里面叫‘zhangsan’的人)
select *from stu where name=‘zhangsan’;
删除指定属性的记录(比如将分数位59并且姓名为‘lisi’的记录删掉)
delete from stu where score=59 and name='lisi';
修改记录(将姓名为‘zhangsan’的记录的分数修改为59)
update stu set score=59 where name='zhangsan';
添加属性(给stu表添加一条addr属性)
alter table stu add column addr char;
复制表(复制表可以全部复制,也可以复制一部分,比如将stu的id,name,score属性复制到stu1表中)
create table stu1 as select id,name,score from stu;
删除一张表(将stu删除)
drop table stu;
更改表的名字(将stu1改成stu)
alter table stu1 rename to stu;
查看当前数据库
.databases
查看当前数据库有多少表
.table
查看表结构(查看表的具体结构)
.schema
使用以下API前
编译的时候加上 -lsqlite3
编译需要先下载库:sudo apt-get install libsqlite3-dev
1 打开数据库,获得该数据库的句柄(句柄不是返回值获得,是更改指针获得的)
int sqlite3_open(char *filename,sqlite3** db);
打开sqlite数据库
参数
返回值:成功返回0,失败返回错误码(非零)
2. 关闭数据库
int sqlite3_close(sqlite3**db);
关闭sqlite数据库
参数
返回值:成功返回0,失败返回错误码(非零)
3. 获得一个字符串的基地址,该字符串就是错误信息
const char *sqlite3_errmsg(sqlite3 **db);
获得错误信息
参数
返回值:错误信息的基地址
4.:执行sql语句操作数据库,当执行查询语句时,如果有N条记录,执行完sql语句后就重复执行N遍回调函数。
int sqlite3_exec(
sqlite3* db, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
sqlite3*:打开的数据库 const char *sql:要执行的sql语句
int (*callback)(void*,int,char**,char**):执行sql语句时对应的回调函数
void *:回调函数的第一个参数 char **errmsg:存放错误信息
//
执行sql语句,执行sql命令
参数
返回值:执行成功为0,不成功为错误信息
回调函数:
typedef int(*sqlite_callback)(void* para, int columenCount, char** columnValue, char** columnName);
//参数:
//void* para:是sqlite3_exec函数的第四个参数
//int columenCount,每条记录的字段数,通俗讲就是列数
//char** columnValue每一个字段的值
//char** columnName字段的名字,通俗讲就是字段最上边一排
//filrname:student.c #include <stdio.h> #include <stdlib.h> #include <sqlite3.h> #include <string.h> //create table fun sqlite3* creat_db_table(void) { sqlite3* db; char* errmsg; //1.create databases int num_open = sqlite3_open("Student.db",&db); if(num_open == 0) {//openning db is success printf("open db is success!\n"); }else { printf("open db no success!\n"); printf("%s\n",sqlite3_errmsg(db)); exit(-1); } //2.create table char sql[128] = {0}; sprintf(sql,"create table stu(id Integer, name Char, score Integer);"); int table_num = sqlite3_exec( db, sql, NULL, NULL, &errmsg); if(table_num == 0) {//create table is success printf("create table is success!\n"); }else { printf("%s\n",sqlite3_errmsg(db)); } return db; } //add fun void add(sqlite3* db) { char* errmsg; //1.pre insert char sql[128] = {0}; int id; char name[40]; int score; printf("input id:"); scanf("%d",&id); printf("input name:"); scanf("%s",name); printf("input score:"); scanf("%d",&score); sprintf(sql,"insert into stu values(%d,'%s', %d);",id,name,score); //2.inserting data int table_num = sqlite3_exec( db, sql, NULL, NULL, &errmsg); if(table_num == 0) {//insert success printf("insert is success!\n"); }else { printf("insert no success!\n"); printf("%s\n",sqlite3_errmsg(db)); exit(-1); } } //delete fun void delete(sqlite3* db) { char* errmsg; //1.pre delete char sql[128] = {0}; int id; printf("what you want delete id:"); scanf("%d",&id); sprintf(sql,"delete from stu where id=%d;",id); //2.delete data int table_num = sqlite3_exec( db, sql, NULL, NULL, &errmsg); if(table_num == 0) {//delete success printf("delete success!\n"); }else { printf("%s\n",sqlite3_errmsg(db)); } } //modify fun void modify(sqlite3* db) { char* errmsg; //1.pre modify char sql[128] = {0}; int id; char name[40] = {}; int score; printf("what you want modify row(input id:)"); scanf("%d",&id); printf("input name what you want modify:"); scanf("%s",name); printf("input score what you wangt modify:"); scanf("%d",&score); sprintf(sql,"update stu set score=%d,name='%s' where id=%d;",score,name,id); //2.modify data int table_num = sqlite3_exec( db, sql, NULL, NULL, &errmsg); if(table_num == 0) {//modify success printf("modify success!\n"); }else { printf("%s\n",sqlite3_errmsg(db)); } } int fun1(void *para, int len, char **value, char **name) { for(int i = 0; i < len; i++) { printf("%s:%s\t\t",name[i],value[i]); } putchar('\n'); return 0; } void select_my(sqlite3* db) { char *errmsg; char sql[100] = {}; sprintf(sql,"select *from stu;"); printf("================select success=================\n"); int num = sqlite3_exec(db, sql, fun1, NULL, &errmsg); printf("===============================================\n"); if(num == 0) { //printf("select success\n"); }else { printf("%s\n",sqlite3_errmsg(db)); } } //menu fun void menu() { printf("1.add\n"); printf("2.delete\n"); printf("3.modify\n"); printf("4.select\n"); printf("5.exit\n"); } int main() { int cmd; sqlite3* db; db = creat_db_table(); while(1) { menu(); printf("input cmd:\n"); scanf("%d",&cmd); //getchar(); switch(cmd) { case 1:add(db); break; case 2:delete(db); break; case 3:modify(db); break; case 4:select_my(db); break; case 5: break; default: printf("error!\n"); } if(cmd == 5) { break; } } sqlite3_close(db); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。