00001 #include <sqlite3.h>
00002 #include <cstdlib>
00003 #include <cstdio>
00004 #include <stdexcept>
00005
00006 void checkErr(int returned_code, sqlite3* db, const char* msg, int expected_code = SQLITE_OK)
00007 {
00008 if (returned_code != expected_code) {
00009 char full_msg[512];
00010 snprintf(full_msg, sizeof(full_msg), "%s: %s\n", msg, sqlite3_errmsg(db));
00011 throw std::runtime_error(full_msg);
00012 }
00013 }
00014
00015 int main(int argc, char** argv)
00016 {
00017
00019 sqlite3 *db = NULL;
00020 checkErr(sqlite3_open("mytest.db", &db), db, "Couldn't open database");
00021
00022
00023 const char CREATE_SQL[] =
00024 "CREATE TABLE IF NOT EXISTS places"
00025 "(id INTEGER PRIMARY KEY,"
00026 " stamp INTEGER,"
00027 " map_position_x DOUBLE, map_position_y DOUBLE, map_position_z DOUBLE,"
00028
00030 " keypoints BLOB, descriptors BLOB)";
00031 sqlite3_stmt *create_table_stmt;
00032 checkErr(sqlite3_prepare_v2(db, CREATE_SQL, sizeof(CREATE_SQL), &create_table_stmt, NULL),
00033 db, "Couldn't prepare CREATE TABLE statement");
00034
00035
00036 checkErr(sqlite3_step(create_table_stmt), db, "CREATE TABLE statement not done", SQLITE_DONE);
00037
00038
00039 checkErr(sqlite3_finalize(create_table_stmt), db, "Couldn't finalize CREATE TABLE statement");
00040
00041
00042 const char INSERT_SQL[] = "INSERT INTO places VALUES (NULL, $stamp, $pos_x, $pos_y, $pos_z, $keypts, $descs)";
00043 sqlite3_stmt *insert_stmt;
00044 checkErr(sqlite3_prepare_v2(db, INSERT_SQL, sizeof(INSERT_SQL), &insert_stmt, NULL),
00045 db, "Couldn't prepare INSERT statement");
00046
00047
00048
00049 checkErr(sqlite3_bind_int64(insert_stmt, 1, (sqlite3_int64)567), db, "Couldn't bind stamp");
00050 checkErr(sqlite3_bind_double(insert_stmt, 2, 1.0), db, "Couldn't bind map_position_x");
00051 checkErr(sqlite3_bind_double(insert_stmt, 3, 2.0), db, "Couldn't bind map_position_y");
00052 checkErr(sqlite3_bind_double(insert_stmt, 4, 3.0), db, "Couldn't bind map_position_z");
00053 const char KEYPOINT_DATA[] = "Keypoint data goes in here";
00054 const char DESCRIPTOR_DATA[] = "Descriptor data goes in here";
00055 checkErr(sqlite3_bind_blob(insert_stmt, 5, KEYPOINT_DATA, sizeof(KEYPOINT_DATA), SQLITE_TRANSIENT),
00056 db, "Couldn't bind keypoints");
00057 checkErr(sqlite3_bind_blob(insert_stmt, 6, DESCRIPTOR_DATA, sizeof(DESCRIPTOR_DATA), SQLITE_TRANSIENT),
00058 db, "Couldn't bind descriptors");
00059
00060
00061 checkErr(sqlite3_step(insert_stmt), db, "INSERT statement not done", SQLITE_DONE);
00062
00063 printf("Last insert rowid = %li\n", sqlite3_last_insert_rowid(db));
00064
00066
00067
00068 checkErr(sqlite3_finalize(insert_stmt), db, "Couldn't finalize INSERT statement");
00069
00070
00071 checkErr(sqlite3_close(db), db, "Couldn't close database");
00072
00073 return 0;
00074 }