SQlite3数据库相关相关命令

1)系统命令 以 ‘.’ 开头

.help 帮助手册
.exit  退出
.table  查看当前数据库的有的表格名
.databases 
.schema  查看表的结构属性

2)sql语句 以 ';'结尾

1. 创建表格

create table <table-name> (id integer, age integer, name char, score float);

2. 插入数据

insert into <table-name> values(1001, 15, zhangsan, 89);// 标准插入
insert into <table-name> values (id, age, name) values(1002, 18, lisi);//按列名插入

3. 查看数据库记录

select * from <table-name>; //查看全部
select * from <table-name> were age = 15;//按列名的信息 age=15 的查看
select * from <table-name> were age = 15 and name = lisi;//按多个列名的信息查看
select * from <table-name> were age = 15 or name = lisi;//age = 15 或 name = lisi满足一个条件就输出
select name, id from <table-name>;//指定字段查询
select * from <table-name> where score >= 90 and score <= 100;//查询score[90,100] 之间的信息

4. 删除信息

delete from <table-name> where id = 1005 and name = 'zhangsan';//删除id=1005和name=‘张三’的信息(同时满足)
delete from <table-name> where id = 1005 or name = 'zhangsan';//删除id=1005或name=‘张三’的信息(有一个条件满足就执行)

5. 更新数据

update <table-name> set name = 'wangwu' where id = 1002;//将id是1002的行信息中的name全部改成 'wangwu';

6. 增加一列

alter table <table-name> add column score float;//在<table-name>表名后面添加一列名: score属性为:float

7. 删除一列 (sqlite3 不支持直接删除一列)

1-- 创建一张新表
	create table <new-table-name> as select id, name, score from <table-name>;//从<table-name>表名中选择指定的列(id,name,score)为基础创建<new-table-name>新的表
2-- 删除原有的表
	drop table <table-name>;
3-- 将新表的名字改成原有的旧表的名字
	alter table <new-table-name> rename to <table-name>;//将<table-name>新表明改成<table-name>旧表明

3)sqlite3 数据库 C语言 API

1. 打开数据库

   int sqlite3_open (
      const char *filename,   /* Database filename (UTF-8) */
      sqlite3 **ppDb          /* OUT: SQLite db handle */
    );
    功能:打开数据库
    参数:filename  数据库名称
          ppdb      数据库句柄
    返回值:成功为0 SQLITE_OK ,出错 错误码

2. 关闭数据库

 int sqlite3_close(sqlite3* db);
    功能:关闭数据库
    参数:
    返回值:成功为0 SQLITE_OK ,出错 错误码

3. 错误信息

 const char *sqlite3_errmsg(sqlite3*db);
    功能:得到错误信息的描述

4. 执行一条sql语句

   int sqlite3_exec(
   	    sqlite3* db,                                  /* An open database */
 	    const char *sql,                           /* SQL to be evaluated */
	    int (*callback)(void* arg,int,char**,char**),  /* Callback function */
 	    void * arg,                                    /* 1st argument to callback */
  		char **errmsg                              /* Error msg written here */
  );
  功能:执行一条sql语句
  参数:db  数据库句柄
        sql 		sql语句
        callback    回调函数,只有在查询时,才传参
        arg         为回调函数传递参数
        errmsg      错误消息
  返回值:成功 SQLITE_OK

5. 查询回调函数

查询回调函数:
int (*callback)(void* arg,int ncolumns ,char** f_value,char** f_name),  /* Callback function */
功能:查询语句执行之后,会回调此函数
参数:arg   接收sqlite3_exec 传递来的参数
      ncolumns 列数
      f_value 列的值得地址
      f_name   列的名称
返回值:0

6. 查询函数

int sqlite3_get_table(
  sqlite3 *db,          /* An open database */
  const char *zSql,     /* SQL to be evaluated */
  char ***pazResult,    /* Results of the query */
  int *pnRow,           /* Number of result rows written here */
  int *pnColumn,        /* Number of result columns written here */
  char **pzErrmsg       /* Error msg written here */
);

功能:不需要回调函数的查询函数
参数:
	db  数据库句柄
	zSql 存放sqlite3的语句
	pazResult  存放表中的数据
	pnRow 行
	pnColumn 列
	pzErrmsg 错误信息

小知识:如果结构体中定义的是一级指针,那么你要定义变量取地址的形式,如果是二级指针函数,你要定义一级指针取地址的形式

学生信息实例

student.c

#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>

#define N 128
#define DATABASE "student.db"
int do_insert(sqlite3 *db);
int do_query(sqlite3 *db);
int do_query_1(sqlite3 *db);
int do_delete(sqlite3 *db);
int do_update(sqlite3 *db);

int main (int argc, char *argv[]) {
	sqlite3 *db;
	char *errmsg;
	int n;

	//打开SQlite数据库文件.db
	if (sqlite3_open(DATABASE, &db) != SQLITE_OK) 
		printf("%s\n", errmsg);
	else 
		printf("open DATABASE success.\n");

	//创建一张数据库的表格 stu
	if (sqlite3_exec(db, "create table stu(id integer, name char, sex char, score integer);", 
															NULL, NULL, &errmsg) != SQLITE_OK) {
		printf("%s\n", errmsg);
	} else {
		printf("Create of open table success.\n");
	}
	
	while (1) {
		printf("************************************************\n");
		printf("1: insert  2:query  3: delete  4: update 5: quit\n");
		printf("************************************************\n");
		printf("Please select:");
		scanf("%d", &n);
		getchar();

		switch(n) {
		case 1:
			do_insert(db);//插入信息
			break;
		case 2:
			//do_query(db);//查询
			do_query_1(db);
			break;
		case 3:
			do_delete(db);//删除
			break;
		case 4:
			do_update(db);//更新
			break;
		case 5:
			printf("main exit.\n");
			sqlite3_close(db);
			exit(0);
			break;
		default:
			printf("Invalid data n.\n");
		}
	}
	return 0;
}

int  do_insert(sqlite3 *db)
{
	int id;
	char name[32] = {};
	char sex;
	int score;
	char sql[N] = {};
	char *errmsg;

	printf("Input id: ");
	scanf("%d", &id);

	printf("Input name: ");
	scanf("%s", name);
	getchar();//回收掉垃圾字符

	printf("Input sex: ");
	scanf("%c", &sex);

	printf("Input score: ");
	scanf("%d", &score);

	sprintf(sql, "insert into stu values(%d, '%s','%c', %d )", id, name, sex, score );

	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
		printf("%s\n", errmsg);
	else 
		printf("Insert done.\n");

	return 0;
}

int callback(int* arg,int f_num ,char** f_value,char** f_name) {
	int i;

	for (i = 0; i < f_num; i++) {
		printf("%-8s", f_value[i]);
	}

	putchar(10);

	return 0;
}

int do_query(sqlite3 *db)
{
	char sql[N] = {};
	char *errmsg;

	sprintf(sql, "select * from stu");

	if(sqlite3_exec(db, sql, callback, NULL, &errmsg) != SQLITE_OK)
		printf("%s\n", errmsg);
	else 
		printf("Query done.\n");

	return 0;
}

int do_query_1(sqlite3 *db)
{
	char *errmsg;
	char ** resultp;
	int nrow;
	int ncolumn;

	if(sqlite3_get_table(db, "select * from stu", &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
		printf("%s\n", errmsg);
	else
		printf("query done.\n");

	int i = 0;
	int j = 0;
	int index = ncolumn;

	for(j = 0; j < ncolumn; j++)
	{
		printf("%-10s", resultp[j]);
	}
	putchar(10);

	for(i = 0; i < nrow; i++)
	{
		for(j = 0; j < ncolumn; j++)
		{
			printf("%-10s ", resultp[index++]);
		}
		putchar(10);
	}

	return 0;
}

int do_delete(sqlite3 *db)
{
	char sql[N] = {};
	char *errmsg;
	int id;

	printf("Invalid id:");
	scanf("%d", &id);
	getchar();

	sprintf(sql, "delete from stu where id = %d;", id);

	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
		printf("%s\n", errmsg);
	else 
		printf("Delete done.\n");


	return 0;
}

int do_update(sqlite3 *db)
{
	char *errmsg;
	char sql[N] = {};
	char name[32] = "zhangsan";
	int id;

	printf("Input id:");
	scanf("%d", &id);

	sprintf(sql, "update stu set name = '%s' where id = %d;",name, id);

	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
		printf("%s\n", errmsg);
	else 
		printf("Delete done.\n");

	return 0;
}

输出结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLite是一种嵌入式数据库引擎,可以通过使用SQLite提供的API来执行数据库操作命令。其中,sqlite3_exec是一个常用的函数,用于执行SQL命令。它的原型如下:sqlite3_exec(sqlite3* db, const char *sql, sqlite_callback, void *data, char **errmsg)。\[1\] 要创建一个SQLite数据库文件,可以使用以下命令sqlite3 testDB.db。这将在当前目录下创建一个名为testDB.db的数据库文件,该文件将被SQLite引擎用作数据库。创建成功后,您将看到一个sqlite>提示符。\[2\] 以下是一个使用SQLite API进行数据库操作的示例代码: #include <stdio.h> #include <sqlite3.h> int main(int argc, char* argv\[\]) { sqlite3 *db; char *zErrMsg = 0; int rc; rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully\n"); } // 在这里可以执行其他数据库操作命令 sqlite3_close(db); } 在上述示例代码中,sqlite3_open函数用于打开名为test.db的数据库文件。如果打开失败,将打印错误信息。如果打开成功,将打印"Opened database successfully"。在打开数据库后,您可以在注释部分执行其他数据库操作命令。最后,使用sqlite3_close函数关闭数据库连接。\[3\] 请注意,上述示例代码只是一个简单的示例,您可以根据具体需求使用SQLite提供的其他API函数来执行更复杂的数据库操作。 #### 引用[.reference_title] - *1* *2* *3* [sqlite3 常用命令](https://blog.csdn.net/yuezhilangniao/article/details/124898271)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值