参考: https://zetcode.com/db/sqlitec/
准备工作:
sudo apt-get install libsqlite3-dev sqlite3 -y
version.c
#include <sqlite3.h>
#include <stdio.h>
int main(void) {
printf("%s\n", sqlite3_libversion());
return 0;
}
编译/运行结果:
$ gcc -o version version.c -lsqlite3
$ ./version
3.22.0
版本号显示正常!
离线
通过SQL脚本命令SQLITE_VERSION和内存数据库方式获取sqlite版本号:
version2.c
#include <sqlite3.h>
#include <stdio.h>
int main(void) {
sqlite3 *db;
sqlite3_stmt *res;
int rc = sqlite3_open(":memory:", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
rc = sqlite3_prepare_v2(db, "SELECT SQLITE_VERSION()", -1, &res, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to fetch data: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
rc = sqlite3_step(res);
if (rc == SQLITE_ROW) {
printf("%s\n", sqlite3_column_text(res, 0));
}
sqlite3_finalize(res);
sqlite3_close(db);
return 0;
}
$ gcc -o version2 version2.c -lsqlite3
$ ./version2
3.22.0
版本号显示正常!
离线
用C编程插入数据:
insert_data.c
#include <sqlite3.h>
#include <stdio.h>
int main(void) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("test.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "DROP TABLE IF EXISTS Cars;"
"CREATE TABLE Cars(Id INT, Name TEXT, Price INT);"
"INSERT INTO Cars VALUES(1, 'Audi', 52642);"
"INSERT INTO Cars VALUES(2, 'Mercedes', 57127);"
"INSERT INTO Cars VALUES(3, 'Skoda', 9000);"
"INSERT INTO Cars VALUES(4, 'Volvo', 29000);"
"INSERT INTO Cars VALUES(5, 'Bentley', 350000);"
"INSERT INTO Cars VALUES(6, 'Citroen', 21000);"
"INSERT INTO Cars VALUES(7, 'Hummer', 41400);"
"INSERT INTO Cars VALUES(8, 'Volkswagen', 21600);";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
return 0;
}
$ gcc -o inser_data inser_data.c -lsqlite3
$ ./inser_data
3.22.0
用sqlite3命令查看运行结果:
$ sqlite3 test.db
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> .tables
Cars
sqlite> select * from Cars
...> ;
1|Audi|52642
2|Mercedes|57127
3|Skoda|9000
4|Volvo|29000
5|Bentley|350000
6|Citroen|21000
7|Hummer|41400
8|Volkswagen|21600
sqlite> .exit
$
$
离线
last_row_id.c
#include <sqlite3.h>
#include <stdio.h>
int main(void) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open(":memory:", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "CREATE TABLE Friends(Id INTEGER PRIMARY KEY, Name TEXT);"
"INSERT INTO Friends(Name) VALUES ('Tom');"
"INSERT INTO Friends(Name) VALUES ('Rebecca');"
"INSERT INTO Friends(Name) VALUES ('Jim');"
"INSERT INTO Friends(Name) VALUES ('Roger');"
"INSERT INTO Friends(Name) VALUES ('Robert');";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "Failed to create table\n");
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
} else {
fprintf(stdout, "Table Friends created successfully\n");
}
int last_id = sqlite3_last_insert_rowid(db);
printf("The last Id of the inserted row is %d\n", last_id);
sqlite3_close(db);
return 0;
}
运行结果:
$ gcc -o last_row_id last_row_id.c -lsqlite3
$
$ ./last_row_id
Table Friends created successfully
The last Id of the inserted row is 5
离线
select_all.c
#include <sqlite3.h>
#include <stdio.h>
int callback(void *, int, char **, char **);
int main(void) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("test.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n",
sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "SELECT * FROM Cars";
rc = sqlite3_exec(db, sql, callback, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "Failed to select data\n");
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
return 0;
}
int callback(void *NotUsed, int argc, char **argv,
char **azColName) {
NotUsed = 0;
for (int i = 0; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
编译运行结果:
$ gcc -o select_all select_all.c -lsqlite3
$
$ ./select_all
Id = 1
Name = Audi
Price = 52642
Id = 2
Name = Mercedes
Price = 57127
Id = 3
Name = Skoda
Price = 9000
Id = 4
Name = Volvo
Price = 29000
Id = 5
Name = Bentley
Price = 350000
Id = 6
Name = Citroen
Price = 21000
Id = 7
Name = Hummer
Price = 41400
Id = 8
Name = Volkswagen
Price = 21600
$
$
离线
通过回调函数,把数据表所有数据输出到 csv(逗号分隔符) 文件
select_all_export_to_csv.c
#include <sqlite3.h>
#include <stdio.h>
FILE* fp_csv;
int callback(void *, int, char **, char **);
int main(void) {
sqlite3 *db;
char *err_msg = 0;
fp_csv = fopen("test.csv", "w");
if(!fp_csv)
{
fprintf(stderr, "Cannot open test.csv\n");
return 2;
}
int rc = sqlite3_open("test.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n",
sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "SELECT * FROM Cars";
rc = sqlite3_exec(db, sql, callback, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "Failed to select data\n");
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
fclose(fp_csv);
return 0;
}
int callback(void *NotUsed, int argc, char **argv,
char **azColName) {
NotUsed = 0;
for (int i = 0; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
fprintf(fp_csv, "%s,", argv[i] ? argv[i] : "NULL");
}
printf("\n");
fprintf(fp_csv, "\n");
return 0;
}
gcc -o select_all_export_to_csv select_all_export_to_csv.c -lsqlite3
$
$ ./select_all_export_to_csv
Id = 1
Name = Audi
Price = 52642
Id = 2
Name = Mercedes
Price = 57127
Id = 3
Name = Skoda
Price = 9000
Id = 4
Name = Volvo
Price = 29000
Id = 5
Name = Bentley
Price = 350000
Id = 6
Name = Citroen
Price = 21000
Id = 7
Name = Hummer
Price = 41400
Id = 8
Name = Volkswagen
Price = 21600
一切OK!
离线