C++DB is the database layer that was designed to work with C++CMS. This customized version is used within Ye Ol' Pi Shack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

86 lines
2.3 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // Distributed under:
  6. //
  7. // the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // or (at your opinion) under:
  12. //
  13. // The MIT License
  14. // (See accompanying file MIT.txt or a copy at
  15. // http://www.opensource.org/licenses/mit-license.php)
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <cppdb/frontend.h>
  19. #include <iostream>
  20. #include <ctime>
  21. int main()
  22. {
  23. try {
  24. cppdb::session sql("sqlite3:db=db.db");
  25. sql << "DROP TABLE IF EXISTS test" << cppdb::exec;
  26. sql<< "CREATE TABLE test ( "
  27. " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
  28. " n INTEGER,"
  29. " f REAL, "
  30. " t TIMESTAMP,"
  31. " name TEXT "
  32. ") " << cppdb::exec;
  33. std::time_t now_time = std::time(0);
  34. std::tm now = *std::localtime(&now_time);
  35. cppdb::statement stat;
  36. stat = sql <<
  37. "INSERT INTO test(n,f,t,name) "
  38. "VALUES(?,?,?,?)"
  39. << 10 << 3.1415926565 << now <<"Hello 'World'";
  40. stat.exec();
  41. std::cout<<"ID: "<<stat.last_insert_id() << std::endl;
  42. std::cout<<"Affected rows "<<stat.affected()<<std::endl;
  43. stat.reset();
  44. stat.bind(20);
  45. stat.bind_null();
  46. stat.bind(now);
  47. stat.bind("Hello 'World'");
  48. stat.exec();
  49. cppdb::result res = sql << "SELECT id,n,f,t,name FROM test";
  50. while(res.next()) {
  51. double f=-1;
  52. int id,k;
  53. std::tm atime;
  54. std::string name;
  55. res >> id >> k >> f >> atime >> name;
  56. std::cout <<id << ' '<<k <<' '<<f<<' '<<name<<' '<<asctime(&atime)<< std::endl;
  57. }
  58. res = sql << "SELECT n,f FROM test WHERE id=?" << 1 << cppdb::row;
  59. if(!res.empty()) {
  60. int n = res.get<int>("n");
  61. double f=res.get<double>(1);
  62. std::cout << "The values are " << n <<" " << f << std::endl;
  63. }
  64. }
  65. catch(std::exception const &e) {
  66. std::cerr << "ERROR: " << e.what() << std::endl;
  67. return 1;
  68. }
  69. return 0;
  70. }