Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <sqlite3.h>
  2.  
  3. should contain sqlite3_prepare_v2 and struct sqlite3. Make sure you're including the right sqlite3.h file.
  4.  
  5. Also in sqlite3_prepare_v2 the 3rd arg can be (and should be in your case) -1 so the sql is read to the first null terminator.
  6.  
  7. Working bare-metal sample using sqlite 3.7.11:
  8.  
  9. #include <sqlite3.h>
  10. int test()
  11. {
  12. sqlite3* pDb = NULL;
  13. sqlite3_stmt* query = NULL;
  14. int ret = 0;
  15. do // avoid nested if's
  16. {
  17. // initialize engine
  18. if (SQLITE_OK != (ret = sqlite3_initialize()))
  19. {
  20. printf("Failed to initialize library: %d\n", ret);
  21. break;
  22. }
  23. // open connection to a DB
  24. if (SQLITE_OK != (ret = sqlite3_open_v2("test.db", &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)))
  25. {
  26. printf("Failed to open conn: %d\n", ret);
  27. break;
  28. }
  29. // prepare the statement
  30. if (SQLITE_OK != (ret = sqlite3_prepare_v2(pDb, "SELECT 2012", -1, &query, NULL)))
  31. {
  32. printf("Failed to prepare insert: %d, %s\n", ret, sqlite3_errmsg(pDb));
  33. break;
  34. }
  35. // step to 1st row of data
  36. if (SQLITE_ROW != (ret = sqlite3_step(query))) // see documentation, this can return more values as success
  37. {
  38. printf("Failed to step: %d, %s\n", ret, sqlite3_errmsg(pDb));
  39. break;
  40. }
  41. // ... and print the value of column 0 (expect 2012 here)
  42. printf("Value from sqlite: %s", sqlite3_column_text(query, 0));
  43.  
  44. } while (false);
  45. // cleanup
  46. if (NULL != query) sqlite3_finalize(query);
  47. if (NULL != pDb) sqlite3_close(pDb);
  48. sqlite3_shutdown();
  49. return ret;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement