SQLite

SQLite

类型关联式数据库
授权协议公有领域
网站https://sqlite.org/

SQLite是关联式数据库管理系统。它嵌入到程序中。

数据类型

INTEGER64位元整数
REAL64位元浮点数
TEXT变长字符串 (UTF-8/16)
BLOB变长二进数据

SQL范例程式

-- Concat strings.
SELECT 'Hello,' || 'world';

-- Append "" to the identifier to distinguish from reserved words.
SELECT "IN" FROM "WHERE";

-- If specified record exists then insert, nor append. (PRIMARY KEY required)
REPLACE INTO page (title,contents) VALUES(?,?);

C语言范例程式

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


int main(int argc,char **argv)
{
  sqlite3 *db;
  if (sqlite3_open("test.db", &db) != SQLITE_OK){
    fprintf(stderr, "cannot open test.db\n");
    exit(1);
  }

  sqlite3_stmt *st;
  if (sqlite3_prepare_v2(db, "SELECT name FROM book WHERE price>?", -1, &st, NULL) != SQLITE_OK) {
    fprintf(stderr, "%s\n", sqlite3_errmsg(db));
    exit(1);
  }

  // Set first place holder to 100
  sqlite3_bind_int(st, 0, 100);

  while (sqlite3_step(st) == SQLITE_ROW) {
    printf("%s\n", sqlite3_column_text(st, 0));
  }
  sqlite3_finalize(st);
  sqlite3_close(db);

  return 0;
}