test-sqlite3-transaction.nut 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. auto db = SQLite3(":memory:");
  2. db.exec_dml("create table test(id integer primary key, value text)");
  3. auto stmt_insert = db.prepare("insert into test(value) values(?)");
  4. stmt_insert.bind_exec("dad");
  5. auto stmt = db.prepare("select * from test");
  6. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  7. auto transaction = db.transaction();
  8. print(transaction);
  9. stmt_insert.bind_exec("car");
  10. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  11. transaction.rollback();
  12. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  13. transaction = db.transaction();
  14. print(transaction);
  15. stmt_insert.bind_exec("car2");
  16. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  17. transaction.commit();
  18. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  19. {
  20. auto transaction3 = db.transaction();
  21. print(transaction3);
  22. auto stmt_sr = db.stmt_scope_reset(stmt_insert);
  23. stmt_insert.bind(1, "car3");
  24. stmt_insert.step();
  25. stmt_insert.reset(); //redundant to test double reset trough stmt_scope_reset
  26. //stmt_insert.bind_exec("car3");
  27. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  28. }
  29. print("after block transaction");
  30. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  31. {
  32. auto transaction4 = db.transaction();
  33. print(transaction4);
  34. auto stmt_sr = db.stmt_scope_reset(stmt_insert);
  35. stmt_insert.bind(1, "car4");
  36. stmt_insert.step();
  37. //stmt_insert.bind_exec("car4");
  38. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));
  39. }
  40. print("End result");
  41. while(stmt.next_row()) print(stmt.col(0), stmt.col(1));