fork download
  1.  
  2.  
  3. void db_insert_into_db(char const *timestr char const *typestr, int ctrstr) {
  4.  
  5.  
  6. std::string filename = "local/errorlog.txt";
  7. std::ofstream outfile;
  8. outfile.open(filename, std::ios_base::app);
  9.  
  10. int rc;
  11. char *exec_errmsg;
  12.  
  13. const char dbname[] = "local/log.db";
  14.  
  15. sqlite3 *db = NULL;
  16.  
  17. rc = sqlite3_open(dbname, &db);
  18. if(SQLITE_OK != rc) {
  19. outfile << "Can't open database "<< dbname << " (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  20. sqlite3_close(db);
  21. exit(1);
  22. }
  23.  
  24. const char insert_sql[] = "INSERT INTO log (thedate, thetype, thecounter) VALUES (?,?,?)";
  25. sqlite3_stmt *insert_stmt = NULL;
  26.  
  27.  
  28. rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, 0);
  29. while ( rc != SQLITE_OK) {
  30. sqlite3_busy_timeout(db, 230);
  31. outfile << "Zzzzz in while" << std::endl;
  32. break;
  33. } //while
  34.  
  35. // prepare_v2
  36. rc = sqlite3_prepare_v2(db, insert_sql, -1, &insert_stmt, NULL);
  37. if(SQLITE_OK != rc) {
  38. outfile << "A Can't prepare insert statment " << insert_sql << " (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  39. sqlite3_close(db);
  40. }
  41.  
  42. rc = sqlite3_bind_text(insert_stmt, 1, timestr, strlen(timestr), NULL);
  43. if(SQLITE_OK != rc) {
  44. outfile << "B Error binding value in insert (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  45. sqlite3_close(db);
  46. }
  47.  
  48. rc = sqlite3_bind_text(insert_stmt, 2, typestr, strlen(typestr), NULL);
  49. if(SQLITE_OK != rc) {
  50. outfile << "C Error binding value in insert (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  51. sqlite3_close(db);
  52. }
  53.  
  54. rc = sqlite3_bind_int(insert_stmt, 3, ctrstr);
  55. if(SQLITE_OK != rc) {
  56. outfile << "D Error binding value in insert (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  57. sqlite3_close(db);
  58. }
  59.  
  60. // step
  61. rc = sqlite3_step(insert_stmt);
  62. if(SQLITE_DONE != rc) {
  63. outfile << "H insert statement didn't return DONE (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  64. }
  65.  
  66. rc = sqlite3_finalize(insert_stmt);
  67. if(SQLITE_OK != rc) {
  68. outfile << "I Error finalize (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  69. sqlite3_close(db);
  70. }
  71.  
  72. rc = sqlite3_close(db);
  73. if(SQLITE_OK != rc) {
  74. outfile << "J Error close db (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
  75. sqlite3_close(db);
  76. }
  77.  
  78. // commit all to sqlite
  79. sqlite3_exec(db, "COMMIT", 0, 0, 0);
  80.  
  81. outfile.close();
  82.  
  83. };
  84.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty