40_insandsel.c 620 B

123456789101112131415161718192021222324252627
  1. #include <sqlite3.h>
  2. #include <stdio.h>
  3. int main()
  4. {
  5. sqlite3 *db;
  6. sqlite3_stmt *res;
  7. sqlite3_open(":memory:", &db);
  8. sqlite3_exec(db, "CREATE TABLE strings(s TEXT)", 0, 0, 0);
  9. for (int i = 0; i < 10; i++)
  10. {
  11. char tmp[1024];
  12. sprintf(tmp, "INSERT INTO strings VALUES ('string%d')", i);
  13. sqlite3_exec(db, tmp, 0, 0, 0);
  14. }
  15. sqlite3_prepare_v2(db, "SELECT * FROM strings", -1, &res, NULL);
  16. printf("Database contents:\n");
  17. int rc = sqlite3_step(res);
  18. while (rc != SQLITE_DONE)
  19. {
  20. printf("%s\n", sqlite3_column_text(res, 0));
  21. rc = sqlite3_step(res);
  22. }
  23. sqlite3_finalize(res);
  24. sqlite3_close(db);
  25. return 0;
  26. }