Linux下Sqlite3数据库操作

目录

一、安装步骤

 二、Sqlite3 命令行操作

常用SQL语句的使用

三、SQLite数据库的API函数

        实例参考:Linux下的SQLite数据库的基本使用-CSDN博客


一、安装步骤

1.先从Windows的官网下载压缩包

 

 2.MobaXterm登录虚拟机,将压缩包从Windows传输到虚拟机下

 3.在压缩包的路径下执行解压命令:tar -xzf sqlite-autoconf-3410200.tar.gz 

4.进入sqlite-autoconf-3410200 路径下查看文件目录,发现没有makefile文件,但是有configure这个文件

cd sqlite-autoconf-3410200/

ls

输入命令./configure --help了解一下configure

 可以发现 --prefix 这个选项可以用来指定安装路径,否则他将默认安装到 /usr/local

5.生成makefile

执行命令:./configure --prefix=`pwd`/../install --disable-static
将其安装到你想要的指定路径下(这里需要注意是绝对路径)

6.执行make

7.执行make install

 二、Sqlite3 命令行操作

首先由 sqlite3 命令进入命令行操作模式

在这之前可能会遇到报错:  sqlite3: command not found

没有sudo权限情况下的解决办法:

先找到sqlite3的绝对路径:

再进行配置文件的修改:

vim ~/.bashrc 添加:export PATH=“$PATH:`pwd`

编辑之后执行使环境变量生效的命令:source ~/.bashrc

常用SQL语句的使用

SQLite基本操作命令
命令说明
.table/.ta查看当前数据库下的表
.open [数据库名称] 打开数据库
.database列出附加数据库的名称和文件
.quit/.q/.exit退出数据库
.output查询的结果输出到文件,如:.output demo.txt
.schema查看表结构(显示表格字段和数据)
.help  显示所有命令

1.创建并进入使用数据库

2、显示数据库:      .databases

3.创建表

create table [创建表名](属性1,属性2,属性3……)

如:create table demo(id integer,name varchar(20))

 

4、显示表结构:sqlite> .schema 【表名】

5.在创建的表中插入数据

6.查看表中的数据信息

7.删除数据和删除表

8、获取指定表的索引列表:       sqlite> .indices 【表名】

9、从SQL文件导入数据库:       sqlite> .read 【文件名】

10、导出数据库到SQL文件

       sqlite> .output 【文件名】

       sqlite> .dump

       sqlite> .output stdout

11、格式化输出数据到CSV格式:

       sqlite> .output 【文件名.csv】

       sqlite> .separator

       sqlite> .select * from test;

       sqlite> .output stdout

12、从CSV文件导入数据到表中:       sqlite> .import 【文件名.csv】 【表名】

13、备份数据库:       [root@jovan ~]# sqlite3 【数据库名】 .dump > backup.sql

14、恢复数据库:      [root@jovan~]# sqlite3 【数据库名】 < backup.sql

三、SQLite数据库的API函数

参考c资料:SQLite3数据库API手册

SqLite3基本操作_sqlite3 操作_JovanDong的博客-CSDN博客

1、该函数用来打开或者创建一个SQLite数据库。

int sqlite3_open(  const char*filename,//数据库名称
                             sqlite3**ppDb//输出参数,SQLite数据库句柄  );
  • 如果在包含该函数的文件所在的路径下有同名的数据库(*.db),则打开数据库;
  • 如果不存在数据库,则在该路径下创建一个同名的数据库。
  • 如果打开或者创建数据库成功,则该函数返回0,输出参数为SQLite3类型的变量,后序对该数据库的操作,通过参数进行传递。

2、关闭数据库。

int sqlite3_close(sqlite3*db);
  • 结束对数据库的操作之后需要调用该函数关闭数据库,该函数的参数为成功打开数据库时输出参数——SQLite3类型变量(句柄)

3、执行函数。

nt sqlite3_exec(
        sqlite3*,//打开数据库的名称
        const char*sql,//要执行的SQL语句
        sqlite_callback,//回调函数,每成功执行一次sql语句就执行一次callback函数
        void*,//回调函数的参数
        char **errmsg//错误信息  )
  •  如果要对数据库进行操作,即可使用该函数来完成。如果出现错误信息的话,可以相应的错误信息保存到errmsg中。

实例参考:Linux下的SQLite数据库的基本使用-CSDN博客

使用SQLite的API函数实现新建一个数据库student.db,并且在数据库中创建表demo,想表中插入数据,查询插入的数据,最后关闭数据库。

第一步:创建一个.c文件sudo touch sqlite_Demo01.c

打开.c文件编写代码:sudo gedit sqlite_Demo01.c

#include<stdio.h>
#include<stdlib.h>
#include<sqlite3.h>
#include<string.h>
 
#define maxn 100
 
//print function
//the name dedicate id and name
//the value dedicate id=1 and name='tom'......
int print(void*arg,int column,char**value,char**name){
	int i;
	for(i=0;i<column;i++){
		printf("%s = %s",name[i],value[i]);
	}
	printf("\n");
	//return 0 represent select successfully
	return 0;		
}
 
int main(){
	//open the database
	sqlite3 *ppdb;
	int ret=sqlite3_open("student.db",&ppdb);
	if(ret!=SQLITE_OK){
		printf("sqlite3_open: %s\n",sqlite3_errmsg(ppdb));
		exit(1);
	}
	//create table
	char sql[maxn]={0};
	sprintf(sql,"create table if not exists demo(id integer primary key,name varchar(20));");
	ret=sqlite3_exec(ppdb,sql,NULL,NULL,NULL);
	if(ret!=SQLITE_OK){
		printf("sqlite3_exec_create_table: %s\n",sqlite3_errmsg(ppdb));
		exit(1);
	}
	
	//insert the data
	int id;
	int i;
	char name[32]={0};
	int n;
	printf("please entry the insert number: ");
	scanf("%d",&n);
	
	for(i=0;i<n;i++){
		printf("please entry the id and name\n");
		scanf("%d %s",&id,name);
		//clean the sql
		memset(sql,0,sizeof(sql));
		sprintf(sql,"insert into demo values(%d,'%s');",id,name);
		//exert the sqlite3_exec
		ret=sqlite3_exec(ppdb,sql,NULL,NULL,NULL);
		if(ret!=SQLITE_OK){
			printf("sqlite3_exec_insert_table: %s\n",sqlite3_errmsg(ppdb));
			exit(1);
		}
	}
	
	//select the table
	memset(sql,0,sizeof(sql));
	sprintf(sql,"select * from demo;");
	ret=sqlite3_exec(ppdb,sql,print,NULL,NULL);
	if(ret!=SQLITE_OK){
		printf("sqlite3_exec_select_table: %s\n",sqlite3_errmsg(ppdb));
		exit(1);
	}
	//the second select method
	char **result;
	int row,column;
	ret=sqlite3_get_table(ppdb,sql,&result,&row,&column,NULL);
	if(ret!=SQLITE_OK){
		printf("sqlite3_exec_select_2_table: %s\n",sqlite3_errmsg(ppdb));
		exit(1);
	}
 
	int j;
	int index=column;
	for(i=0;i<row;i++){
		for(j=0;j<column;j++){
			printf("%s = %s  ",result[j],result[index]);
			index++;
		}
		printf("\n");
	}
    //关闭数据库
    ret=sqlite3_close(ppdb);
    if(ret!=SQLITE_OK){
        printf("sqlite3_exec_close_database: %s\n",sqlite3_errmsg(ppdb));
    }
	return 0;
}

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值