4. Create DB/table/index
#define CMD_CREATE_DP_TABLE "CREATE TABLE IF NOT EXISTS `data_planner` ("
"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"`tx_bytes` bigint(20) NOT NULL,"
"`rx_bytes` bigint(20) NOT NULL,"
"`timestamp` datetime);"
#define CMD_CREATE_DP_INDEX "CREATE INDEX IF NOT EXISTS `timestamp_idx` ON
`data_planner` (`timestamp` DESC);"
6. Create DB/table/index
if (sqlite3_exec(db, CMD_CREATE_DP_INDEX, NULL, NULL, &err_msg) != SQLITE_OK) {
syslog(LOG_ERR, "Create data_planner index fail: %s", err_msg);
goto out;
}
ret = 0;
out:
if (err_msg) sqlite3_free(err_msg);
if (db) sqlite3_close(db);
return ret;
7. Create DB/table/index
● Check if using index
● “EXPLAIN <your query>”
○ ex: EXPLAIN SELECT * FROM a WERHE name =
“1”;
○ Will show IdxRowid/IdxGE if apply index
15. Timezone issue
● Use UTC
● In C
○ Use gmtime_r instead of localtime_r
● In SQLite3
○ Insert using datetime(?, 'unixepoch')
■ ? is time_t
○ Query using datetime(timestamp, 'localtime')
16. SQL Debug
void* sqlite3_trace( sqlite3* db, trace_callback, void* udp);
// Register sql callback and call it every time before executing
void trace_callback( void* udp, const char* sql );
// udp means user data pointer
17. SQLite Busy Handle
● SQlite may be locked sometimes, solution:
○ sqlite3_busy_handle
■ More flexible, can add sleep() and retry times
○ sqlite3_busy_timeout
○ http://www.bubuko.com/infodetail-240892.html
○ http://www.360doc.
com/content/10/1214/12/87000_77984300.shtml