Posted By

ktrout on 11/03/13


Tagged

database c create SQLite


Versions (?)

sqlite create new database


 / Published in: C
 

Source creating a new database (and inserting some records)

  1. #include <sqlite3.h>
  2. #include <stdio.h>
  3.  
  4. #define safe_exit() sqlite3_close(db);db=0;return 0
  5. #define _tempdb "temp.db"
  6.  
  7. int error;
  8.  
  9. int main()
  10. {
  11. sqlite3 *db; char sql[300]; char *err_msg;
  12. error = sqlite3_open(_tempdb, &db);
  13. if(error!=SQLITE_OK)
  14. {
  15. safe_exit();
  16. }
  17. error = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS \"memory\" (\"order\" INTEGER PRIMARY KEY,\"address\" INTEGER,\"size\" INTEGER,\"file\" TEXT NOT NULL,\"line\" INTEGER,\"typ\" INTEGER);", 0, 0, 0);
  18. if(error!=SQLITE_OK)
  19. {
  20. safe_exit();
  21. }
  22. sprintf(sql,"INSERT INTO \"memory\" (\"address\",\"size\",\"file\",\"line\",\"typ\") VALUES (1442040,20,'fname',22,1);");
  23. error = sqlite3_exec(db, sql, 0, 0, &err_msg);
  24. if(error!=SQLITE_OK)
  25. {
  26. fprintf(stdout,"error: %s\n",err_msg);
  27. }
  28. safe_exit();
  29. }

Report this snippet  

You need to login to post a comment.