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;
}