Advertisement
zinosat

mongoose + sqlite

Feb 16th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. static void handle_existing_db_request(struct mg_connection *nc, void *ev_data)
  2. {
  3. struct http_message *hm = (struct http_message *) ev_data;
  4. sqlite3_stmt *stmt = NULL;
  5. (void) hm;
  6.  
  7. mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
  8.  
  9. if (sqlite3_prepare_v2(s_db_handle, "SELECT mac,mac_vendor FROM t_host;", -1, &stmt, NULL) == SQLITE_OK) {
  10. mg_printf_http_chunk(nc, "<table>\r\n");
  11. while (1) {
  12. int s, i;
  13.  
  14. mg_printf_http_chunk(nc, "<rd>\r\n");
  15. s = sqlite3_step (stmt);
  16. if (s == SQLITE_ROW) {
  17. for (i=0; i<2; i++) {
  18. const unsigned char *text = sqlite3_column_text (stmt, i);;
  19. mg_printf_http_chunk(nc, "<td>%s</td>", text);
  20. }
  21. mg_printf_http_chunk(nc, "\r\n");
  22. }
  23. else if (s == SQLITE_DONE) {
  24. break;
  25. }
  26. else {
  27. fprintf (stderr, "Failed.\n");
  28. exit (1);
  29. }
  30. mg_printf_http_chunk(nc, "</rd>\r\n");
  31. }
  32.  
  33. mg_printf_http_chunk(nc, "</table>\r\n");
  34. }
  35.  
  36. mg_send_http_chunk(nc, "", 0);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement