SHARE
TWEET

Untitled

a guest Jun 14th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <soci/soci.h>
  2. #include <iostream>
  3. #include <istream>
  4. #include <ostream>
  5. #include <string>
  6. #include <exception>
  7.  
  8. using namespace soci;
  9. using namespace std;
  10.  
  11. /*
  12.  * mysql> desc test;
  13.  * +-------+-------------+------+-----+---------+----------------+
  14.  * | Field | Type        | Null | Key | Default | Extra          |
  15.  * +-------+-------------+------+-----+---------+----------------+
  16.  * | id    | int(50)     | NO   | PRI | NULL    | auto_increment |
  17.  * | name  | varchar(50) | YES  |     | NULL    |                |
  18.  * +-------+-------------+------+-----+---------+----------------+
  19.  */
  20. struct Person
  21. {
  22.     int id;
  23.     std::string name;
  24. };
  25.  
  26. namespace soci
  27. {
  28. template<> struct type_conversion<Person>
  29. {
  30.     typedef values base_type;
  31.     static void from_base(values const & v, indicator /* ind */, Person & p)
  32.     {
  33.         p.id = v.get<int>("id");
  34.         p.name = v.get<std::string>("name");
  35.     }
  36.     static void to_base(const Person & p, values & v, indicator & ind)
  37.     {
  38.     cout << "setting id to " << p.id << " and name to '" << p.name << "'" << endl;
  39.         v.set("id", p.id);
  40.         v.set("name", p.name);
  41.         ind = i_ok;
  42.     }
  43. };
  44. }
  45.  
  46. void purge(session &sql)
  47. {
  48.     int count;
  49.         sql << "select count(*) from test", into(count);
  50.     if (count > 0) {
  51.             cout << "We have " << count << " entries in test: deleting.";
  52.         sql << "DELETE FROM test";
  53.     } else {
  54.         cout << "No entry in test." << endl;
  55.     }
  56. }
  57.  
  58. void insert(session &sql)
  59. {
  60.     purge(sql);
  61.     Person p;
  62.     p.id = -1; p.name = "default";
  63.     statement st = (sql.prepare << "insert into test(id, name) values(:id, :name)", use(p));
  64.     sql.begin();   
  65.     for (int i=1; i <= 10000; i++) {
  66.         p.id = i;
  67.         ostringstream sname;
  68.         sname << "NameNumber" << i;
  69.         p.name = sname.str();
  70.         cout << "Trying to insert id=" << p.id << " and name=" << p.name << endl;
  71.         st.execute(true);
  72.     }
  73.     sql.commit();
  74. }
  75.  
  76. void select(session &sql)
  77. {
  78.     Person p;
  79.     statement st = (sql.prepare << "select id, name from test", into(p));
  80.     st.execute();
  81.     while (st.fetch()) {
  82.         cout << "Id=" << p.id << " Name=" << p.name << endl;
  83.     }
  84. }
  85.  
  86. int main()
  87. {
  88.     try
  89.     {
  90.         session sql("mysql", "dbname=test user=root password=''");
  91.        
  92.     insert(sql);
  93.     select(sql);
  94.     }
  95.     catch (exception const &e)
  96.     {
  97.         cerr << "Error: " << e.what() << '\n';
  98.     }
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top