|
下面将介绍如何快速体验SQLite并且不需要做大量的阅读和配置。
Here is what you do to start experimenting with SQLite without having
to do a lot of tedious reading and configuration:
获取一个适合你机器的预编译二进制包或者获取一份源码然后自己编译。请到 下载 页获取更多信息。
Get a copy of the prebuilt binaries for your machine, or get a copy
of the sources and compile them yourself. Visit
the download page for more information.
如果使用命令行只需要输入 "sqlite3 test.db" 。就会创建一个名为 "test.db" 的新数据库(你可以使用任何你喜欢的名字)。
At a shell or DOS prompt, enter: "sqlite3 test.db". This will
create a new database named "test.db". (You can use a different name if
you like.)
立即输入SQL命令来构建数据库。
Enter SQL commands at the prompt to create and populate the
new database.
下面是一个讲解如何通过TCL接口使用SQLite的一个简单的 TCL 程序。 Below is a simple
TCL program that demonstrates how to use
the TCL interface to SQLite. 该程序会在由第一个参数定义的数据库上执行第二个参数输入的SQL语句。The program executes the SQL statements
given as the second argument on the database defined by the first
argument. 下面命令中,第7行的 sqlite3 命令创建了一个名为 "db" 的对象用来打开并访问SQLite数据库,第8行中 db 对象的 eval 方法用来在数据库上执行SQL命令,最后一行脚本负责关闭了数据库连接。
The commands to watch for are the sqlite3 command
on line 7 which opens an SQLite database and creates
a new object named "db" to access that database, the
use of the eval method on the db object on line 8 to run
SQL commands against the database, and the closing of the database connection
on the last line of the script.
01 #!/usr/bin/tclsh 02 if {$argc!=2} { 03 puts stderr "Usage: %s DATABASE SQL-STATEMENT" 04 exit 1 05 } 06 package require sqlite3 07 sqlite3 db [lindex $argv 0] 08 db eval [lindex $argv 1] x { 09 foreach v $x(*) { 10 puts "$v = $x($v)" 11 } 12 puts "" 13 } 14 db close
下面是一个展示如何使用 C/C++ 接口访问SQLite的c程序。第一个参数指定数据库名称,第二个参数指定需要执行的一或多条SQL语句。需要注意的函数调用是第22行的sqlite3_open()负责打开数据库,第28行的sqlite3_exec()用来在数据库上执行SQL命令,第33行的sqlite3_close()用来关闭数据库连接。
Below is a simple C program that demonstrates how to use
the C/C++ interface to SQLite. The name of a database is given by
the first argument and the second argument is one or more SQL statements
to execute against the database. The function calls to pay attention
to here are the call to sqlite3_open() on line 22 which opens
the database, sqlite3_exec() on line 28 that executes SQL
commands against the database, and sqlite3_close() on line 33
that closes the database connection.
SQLite C/C++ 接口入门页包含几十个SQLite接口函数和概述。
See also the Introduction To The SQLite C/C++ Interface for
an introductory overview and roadmap to the dozens of SQLite interface
functions.
01 #include <stdio.h> 02 #include <sqlite3.h> 03 04 static int callback(void *NotUsed, int argc, char **argv, char **azColName){ 05 int i; 06 for(i=0; i<argc; i++){ 07 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 08 } 09 printf("\n"); 10 return 0; 11 } 12 13 int main(int argc, char **argv){ 14 sqlite3 *db; 15 char *zErrMsg = 0; 16 int rc; 17 18 if( argc!=3 ){ 19 fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]); 20 return(1); 21 } 22 rc = sqlite3_open(argv[1], &db); 23 if( rc ){ 24 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); 25 sqlite3_close(db); 26 return(1); 27 } 28 rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg); 29 if( rc!=SQLITE_OK ){ 30 fprintf(stderr, "SQL error: %s\n", zErrMsg); 31 sqlite3_free(zErrMsg); 32 } 33 sqlite3_close(db); 34 return 0; 35 }