执行openwrt的make menuconfig,选择sqlite并编译,然后就可以使用sqlite了。
下面是一段初始化sqlite数据库的简单代码,可以在sqlite数据库里创建一个表并插入一条记录:
#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 init_db() { sqlite3 *db; char *zErrMsg = 0; int rc; char *sql; rc = sqlite3_open("test.db", &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return -1; } /* Create SQL statement */ sql = "CREATE TABLE example_table(" \ "ID INT PRIMARY KEY NOT NULL," \ "date DATETIME NOT NULL," \ "type INT NOT NULL," \ "msg CHAR(200) NOT NULL);"; /* Execute SQL statement */ rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); if (rc != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Table created successfully\n"); } /* Create SQL statement */ sql = "INSERT INTO example_table (ID,date,type,msg) " \ "VALUES (1, date('now'), 1, 'TEST' ); "; /* Execute SQL statement */ rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); if (rc != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Records created successfully\n"); } sqlite3_close(db); return 0; }
本站的文章和资源来自互联网或者站长的原创,按照 CC BY -NC -SA 3.0 CN协议发布和共享,转载或引用本站文章应遵循相同协议。如果有侵犯版权的资 源请尽快联系站长,我们会在24h内删除有争议的资源。欢迎大家多多交流,期待共同学习进步。