Difference between revisions of "SQLite: Cara Cepat Menggunakan SQLite"
Onnowpurbo (talk | contribs) (New page: ==Download The Code== 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. ==Cr...) |
Onnowpurbo (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | ==Download | + | ==Download Code== |
− | + | Ambil copy dari binary sqlite untuk mesin anda, atau ambil source code dan compile. | |
− | |||
− | + | ==Membuat Sebuah Database Baru== | |
− | + | Di shell ketik | |
− | + | sqlite3 test.db | |
− | + | Ini akan membuat database baru dengan nama "test.db". Tentunya. anda dapat menggunakan nama lain yang anda suka. | |
− | + | Masukan perintah SQL di prompt untuk membuat dan mengisi database yang baru tersebut. | |
+ | |||
+ | |||
+ | ==Menulis Program yang menggunakan SQLite== | ||
+ | |||
+ | Below is a simple TCL program that demonstrates how to use the TCL interface to SQLite. The program executes the SQL statements given as the second argument on the database defined by the first argument. The commands to watch for are the sqlite3 command on line 7 which opens an SQLite database and creates a new TCL command named "db" to access that database, the invocation of the db command on line 8 to execute SQL commands against the database, and the closing of the database connection on the last line of the script. | ||
#!/usr/bin/tclsh | #!/usr/bin/tclsh | ||
Line 30: | Line 34: | ||
db close | db 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. | |
− | + | See also the Introduction To The SQLite C/C++ Interface for an introductory overview and roadmap to the dozens of SQLite interface functions. | |
#include <stdio.h> | #include <stdio.h> |
Latest revision as of 09:04, 25 June 2012
Download Code
Ambil copy dari binary sqlite untuk mesin anda, atau ambil source code dan compile.
Membuat Sebuah Database Baru
Di shell ketik
sqlite3 test.db
Ini akan membuat database baru dengan nama "test.db". Tentunya. anda dapat menggunakan nama lain yang anda suka.
Masukan perintah SQL di prompt untuk membuat dan mengisi database yang baru tersebut.
Menulis Program yang menggunakan SQLite
Below is a simple TCL program that demonstrates how to use the TCL interface to SQLite. The program executes the SQL statements given as the second argument on the database defined by the first argument. The commands to watch for are the sqlite3 command on line 7 which opens an SQLite database and creates a new TCL command named "db" to access that database, the invocation of the db command on line 8 to execute SQL commands against the database, and the closing of the database connection on the last line of the script.
#!/usr/bin/tclsh if {$argc!=2} { puts stderr "Usage: %s DATABASE SQL-STATEMENT" exit 1 } load /usr/lib/tclsqlite3.so Sqlite3 sqlite3 db [lindex $argv 0] db eval [lindex $argv 1] x { foreach v $x(*) { puts "$v = $x($v)" } puts "" } db 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.
See also the Introduction To The SQLite C/C++ Interface for an introductory overview and roadmap to the dozens of SQLite interface functions.
#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++){ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); 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: %s\n", zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); return 0; }