Linux学习(二十八):数据库sqlite3
来源:互联网 发布:二手东西交易软件 编辑:程序博客网 时间:2024/05/29 04:09
数据库sqlite3的详细知识参考http://www.runoob.com/sqlite/sqlite-create-database.html或官方网站http://www.sqlite.org/
sqlite3的安装不做介绍,可以从上面的网站查询相关信息
1、创建数据库
创建数据库的命令很简单 sqlite3 name.db
创建一个名字为stu.db的数据库
2、常用命令
sqlite3命令分为两类,系统命令,以 "."开头。普通命令 ,以";"结束
常用的系统命令
.schema 查看表的结构
.quit 退出数据库
.exit 退出数据库
.help 查看帮助信息
.databases 查看数据库
.tables 显示数据库中所有的表的表名
.quit 退出数据库
.exit 退出数据库
.help 查看帮助信息
.databases 查看数据库
.tables 显示数据库中所有的表的表名
上述几个命令比较常用,练习几遍也就记住了
常用的普通命令:
(1)创建一个表
格式:create table 表名(字段名称1 字段类型,字段名称2 字段类型, ....);
可以添加关键字PRIMARY KEY,表示这个字段是关键字段,不允许重复,比如我们在某个网站注册时不允许有相同的用户名。
创建一个学生信息表,表中信息为id,name,sex,score
(2)删除一张表
格式:drop table 表名;
创建一个学生信息表stuinfor2再删除
(3)向表中添加信息
格式: insert into 表名 values (字段值1,字段值2,...);
添加三条信息
(4)查询信息
格式: select 字段值1,字段值2,... from 表名
查询stuinfor表中的id和name信息
查询表中所有信息,select * from 表名
(5)删除信息:
格式:delete from 表名 where 字段值=要删除的字段号;
删除stuinfor表总id号为1002的信息(where是个好东西)
(6)修改信息
格式:update 表名 set 字段号1=字段值,字段号2=字段值 where 字段号=字段值;
修改id为1001的信息中名称为'lilei',score为77
上面四个就是信息的增删改查
(7)添加列
我们在原有列的基础上再添加其他信息
alter table 表名 add column 字段名 字段类型;
在stuinfor表中添加address信息
3、sqlite3 API接口
(1)创建/打开一个数据库
错误码的详细信息见sqlite错误码
(2)执行一条sql语句
回调函数
(3)查询数据库
也有专门的查询数据库的函数
pazResult返回的字符串数量实际上是(*pnRow+1)*(*pnColumn),因为前(*pnColumn)个是字段名,
注意:nrow的值不包括字段名,如果打印时用for (i = 0; i < nrow; i++)会打印出字段名,但是会少打印出一条符合条件的记录。
因此打印时要用 for (i = 0; i <nrow + 1; i++);将包括字段名在内的数据都打印出来。
因此打印时要用 for (i = 0; i <nrow + 1; i++);将包括字段名在内的数据都打印出来。
例程:添加一个学生信息系统,id号为关键字,提供增删改查功能
#include <stdio.h> //printf//#include <arpa/inet.h> //inet_addr htons//#include <sys/types.h>//#include <sys/socket.h> //socket bind listen accept connect//#include <netinet/in.h> //sockaddr_in#include <stdlib.h> //exit#include <unistd.h> //close//#include <string.h>//#include <sys/select.h>//#include <sys/time.h>//#include <sqlite3.h>#include <sqlite3.h>#define N 128#define errlog(errmsg) do{\perror(errmsg);\printf("%s --> %s --> %d\n", __FILE__, __func__, __LINE__);\exit(1);\ }while(0)/*******************************insert information*******************************/void do_insert(sqlite3 *db){int id,score,rc;char *errmsg =NULL;char name[128]={};char sql[128] ={0};printf("please input id num\n");scanf("%d",&id);printf("please input name\n");scanf("%s",name);printf("please input score\n");scanf("%d",&score);sprintf(sql,"insert into stu values(%d,'%s',%d)",id,name,score );if((rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg))!=SQLITE_OK){printf("%s\n", errmsg);}else{printf("add information successfuly \n");}}/*******************************inquiry information*******************************/void do_inquiry(sqlite3 *db){int i,j,nrow,ncolumn,rc,n;char *errmsg =NULL;char **res=NULL;if(rc = sqlite3_get_table(db,"select * from stu",&res,&nrow,&ncolumn,&errmsg)!=SQLITE_OK){errlog("get table err");}n=0;for(i=0;i<nrow+1;i++){for(j=0;j<ncolumn;j++){printf("%-11s",res[n++]);}putchar(10);}}/*******************************delete information*******************************/void do_delete(sqlite3 *db){int id ,rc;char sql [128]={};char *errmsg =NULL;printf("plesae input id num>>>>");scanf("%d",&id);sprintf(sql,"delete from stu where id=%d",id);if((rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg))!=SQLITE_OK){printf("%s\n", errmsg);}else{printf("delete information successfuly \n");}}/*******************************insert information*******************************/void do_change(sqlite3 *db){int id,score,rc;char *errmsg =NULL;char name[128]={};char sql[128] ={0};printf("please input id num\n");scanf("%d",&id);printf("please input name\n");scanf("%s",name);printf("please input score\n");scanf("%d",&score);sprintf(sql,"update stu set name ='%s' ,score = %d where id=%d",name,score,id);if((rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg))!=SQLITE_OK){printf("%s\n", errmsg);}else{printf("delete information successfuly \n");}}/*******************************insert information*******************************/int main(int argc, const char *argv[]){int rc;sqlite3 *db;int data;char *errmsg = NULL;if((rc = sqlite3_open("stu.db",&db))!=SQLITE_OK){errlog("open sqlite err");}/*creat tables*/if((rc = sqlite3_exec(db,"create table stu( id int PRIMARY KEY, name char,score int)",NULL,NULL,&errmsg))!=SQLITE_OK){printf("%s\n", errmsg);}else{printf("create table \n");}while(1){printf("************************************************************\n");printf("****1 insert ,2 inquiry,3 delete, 4change ,5quit************\n");printf("************************************************************\n");printf(">>>>>");scanf("%d",&data);switch(data){case 1:do_insert(db);break;case 2:do_inquiry(db);break;case 3:do_delete(db);break;case 4:do_change(db);break;case 5:return 0;default:break;}}return 0;}
阅读全文
1 0
- Linux学习(二十八):数据库sqlite3
- 学习sqlite3(二)
- Linux数据库(sqlite3)
- Linux学习笔记(二十八、分区格式化)
- 学习笔记(二十八)
- Linux实践工程师学习笔记二十八
- Linux学习笔记二十八Network_hw
- linux学习:sqlite3简单数据库查询操作
- Linux学习笔记(十八)
- php学习笔记(二十八)session的高级管理(基于数据库和memcache的)
- 数据库学习纪要(二十八):MySQL-8-部分数据函数
- 软件工程视频学习笔记(二十八)
- JAVA学习笔记(二十八)
- python 学习笔记(二十八)
- tensorflow学习笔记(二十八):collection
- java学习笔记(二十八)异常
- 信息安全工程师 学习笔记(二十八)
- Linux下数据库sqlite3
- MVP+Retrofit网络请求和添加拦截器+Fresco+全选反选与删除
- 降维方法PCA
- OkHttp封装+网络拦截器
- struts之采用xml的方式来对表单当中的数据进行过滤操作
- 骑士走棋盘(c/python)
- Linux学习(二十八):数据库sqlite3
- 计蒜客 最大子阵
- Lua基础之table(表)
- 图片框架 三重加载
- UTF8编码字节流错误小析
- Java基础面向对象编程
- yum使用报错302--解决办法
- oracle:分析数据
- Apache httpd 目录列表禁用配置(options indexes)