Here is what you do to start experimenting with SQLite without having to do a lot of tedious reading and configuration:
1. 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 everything you need.
2. Create a Database with the CLI
The Command Line Interface (or "CLI") is a simple command-line program that lets you interact with an SQLite database. The program is named sqlite3 (or sqlite3.exe on Windows).
- At a shell or command prompt, enter:
sqlite3 test.db. This will create a new database file namedtest.db. - Enter SQL commands at the prompt to create and populate the new database.
- See the detailed CLI documentation for more.
A WASM build of the CLI that runs in your web-browser is available at https://sqlite.org/fiddle.
3. Write Programs That Use SQLite
Tcl Example
Below is a simple TCL program that demonstrates how to use the TCL interface to SQLite. Note the sqlite3 command on line 7 which opens the database, the eval method to run SQL, and the close command.
#!/usr/bin/tclsh
if {$argc!=2} {
puts stderr "Usage: %s DATABASE SQL-STATEMENT"
exit 1
}
package require sqlite3
sqlite3 db [lindex $argv 0]
db eval [lindex $argv 1] x {
foreach v $x(*) {
puts "$v = $x($v)"
}
puts ""
}
db close
C/C++ Example
Below is a simple C program that demonstrates the C/C++ interface. The key functions are sqlite3_open() on line 22 to open the database, sqlite3_exec() on line 28 to execute SQL, and sqlite3_close() on line 33 to close the connection.
See also the Introduction To The SQLite C/C++ Interface for a roadmap to the API.
#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;
}
See the How To Compile SQLite document for instructions and hints on how to compile the program shown above.