sqlite3学习笔记

Posted by wsxq2 on 2022-03-16
TAGS:  FROM_DOCXsqlite3

本文最后一次编辑时间:2022-03-16 15:31:32 +0800

简介

SQL语句

sqlite3(命令行)

自动补全

重新编译

编译时加入readline的库即可

使用rlwrap

使用rlwrap亦可。创建一个补全文件(如sqlite3_completions),再调用rlwrap命令使用该补全文件即可拥有补全功能及readline的所有其他功能(如上下键历史命令回溯)

sqlite3_completions
1
2
ABORT ACTION ADD AFTER ALL ALTER ANALYZE AND AS ASC ATTACH AUTOINCREMENT BEFORE BEGIN BETWEEN BY CASCADE CASE CAST CHECK COLLATE COLUMN COMMIT CONFLICT CONSTRAINT CREATE CROSS CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP DATABASE DEFAULT DEFERRABLE DEFERRED DELETE DESC DETACH DISTINCT DROP EACH ELSE END ESCAPE EXCEPT EXCLUSIVE EXISTS EXPLAIN FAIL FOR FOREIGN FROM FULL GLOB GROUP HAVING IF IGNORE IMMEDIATE IN INDEX INDEXED INITIALLY INNER INSERT INSTEAD INTERSECT INTO IS ISNULL JOIN KEY LEFT
LIKE LIMIT MATCH NATURAL NO NOT NOTNULL NULL OF OFFSET ON OR ORDER OUTER PLAN PRAGMA PRIMARY QUERY RAISE RECURSIVE REFERENCES REGEXP REINDEX RELEASE RENAME REPLACE RESTRICT RIGHT ROLLBACK ROW SAVEPOINT SELECT SET TABLE TEMP TEMPORARY THEN TO TRANSACTION TRIGGER UNION UNIQUE UPDATE USING VACUUM VALUES VIEW VIRTUAL WHEN WHERE WITH WITHOUT .archive .auth .backup .bail .binary .cd .changes .check .clone .databases .dbconfig .dbinfo .dump .echo .eqp .excel .exit .expert .explain .filectrl .fullschema .headers .help .import .imposter .indexes .limit .lint .load .log .mode .nullvalue .once .open .output .parameter .print .progress .prompt .quit .read .recover .restore .save .scanstats .schema .selftest .separator .session .shell .show .stats .system .tables .testcase .testctrl .timeout .timer .trace .vfsinfo .vfslist .vfsname .width
s(alias)

alias s=’rlwrap -a -N -c -i -f ~/.config/rlwrap/sqlite3_completions sqlite3’

分隔符

sql - How can I specify the record delimiter to be used in SQLite’s output? - Stack Overflow

使用样例

libsqlite3.so(lib库)

使用样例

base.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
  int i;
  for (i = 0; i < argc; i++) {
  }
  return 0;
}

int main(int argc, char **argv) {
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;

  if (argc != 3) {
    fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
    return (1);
  }
  rc = sqlite3_open(argv[1], &db);
  if (rc) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return (1);
  }
  rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "SQL error: %d, %s\n",rc, zErrMsg);
    sqlite3_free(zErrMsg);
  }
  sqlite3_close(db);
  return 0;
}

编译方法

1
gcc -O0 -lsqlite3 a.c

性能优化

查询

参见SQLite锁机制

rbu

The RBU Extension

参考资源

修订记录

修订时间 修订人 版本 说明
TODO wsxq2 1.0 初稿