Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49593
  • 博文数量: 11
  • 博客积分: 85
  • 博客等级: 民兵
  • 技术积分: 163
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-08 09:59
文章分类

全部博文(11)

文章存档

2013年(8)

2012年(3)

我的朋友

分类: C/C++

2013-04-24 18:29:49


点击(此处)折叠或打开

  1. // g++ -Wall -Werror -std=c++0x -lmysqlclient_r -lmysqlcppconn -o test test.cpp
  2. // create database Contacts;
  3. // create table contacts(uid int unsigned auto_increament key, name varchar(32), phone varchar(32));

  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <memory>

  8. #include <mysql_connection.h>
  9. #include <cppconn/driver.h>
  10. #include <cppconn/exception.h>
  11. #include <cppconn/resultset.h>
  12. #include <cppconn/statement.h>

  13. #define DELETE(x) do {if (x) {delete (x); (x) = NULL;}} while(0)

  14. using namespace std;

  15. class MySQLOpr
  16. {
  17. public:
  18.     MySQLOpr() : _con(NULL) {}

  19.     ~MySQLOpr()
  20.     {
  21.         DELETE(_con);
  22.     }

  23. #define SQL_TRY try {
  24. #define SQL_CATCH } catch (sql::SQLException &e) \
  25.         { \
  26.             cerr << "# ERR: SQLException in " << __FILE__; \
  27.             cerr << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; \
  28.             cerr << "# ERR: " << e.what(); \
  29.             cerr << " (MySQL error code: " << e.getErrorCode(); \
  30.             cerr << ", SQLState: " << e.getSQLState() << " )" << endl; \
  31.         }

  32.     int Connect(const char *addr, const char *user, const char *passwd,
  33.             const char *dbname)
  34.     {
  35.         strncpy(_addr, addr, sizeof(_addr));
  36.         strncpy(_user, user, sizeof(_user));
  37.         strncpy(_passwd, passwd, sizeof(_passwd));
  38.         strncpy(_dbname, dbname, sizeof(_dbname));
  39.         sql::Driver *driver = get_driver_instance();
  40.         DELETE(_con);
  41.         SQL_TRY
  42.             _con = driver->connect(addr, user, passwd);
  43.             _con->setSchema(dbname);
  44.         SQL_CATCH
  45.         if (_con)
  46.             return 0;
  47.         return -1;
  48.     }

  49.     int Update(const char *sql)
  50.     {
  51.         assert(_con != NULL);
  52.         sql::Statement *stmt = NULL;
  53.         int n;
  54.         SQL_TRY
  55.             stmt = _con->createStatement();
  56.             n = stmt->executeUpdate(sql);
  57.         SQL_CATCH
  58.         delete stmt;
  59.         return n;
  60.     }

  61.     unique_ptr<sql::ResultSet> Query(const char *sql)
  62.     {
  63.         assert(_con != NULL);
  64.         sql::Statement *stmt = NULL;
  65.         sql::ResultSet *res = NULL;
  66.         SQL_TRY
  67.             stmt = _con->createStatement();
  68.             res = stmt->executeQuery(sql);
  69.         SQL_CATCH
  70.         cout << "ResultSet ptr " << res << endl;
  71.         return unique_ptr<sql::ResultSet>(res);
  72.     }

  73. private:
  74.     sql::Connection *_con;

  75.     char _addr[64];
  76.     char _user[64];
  77.     char _passwd[64];
  78.     char _dbname[64];

  79.     MySQLOpr(const MySQLOpr &);
  80.     MySQLOpr& operator=(const MySQLOpr &);
  81. };

  82. class DBOperator
  83. {
  84. public:
  85.     static DBOperator& Instance()
  86.     {
  87.         static DBOperator _opr;
  88.         return _opr;
  89.     }

  90.     int Init()
  91.     {
  92.         return contactsOpr.Connect("tcp://127.0.0.1:3307", "user",
  93.                 "user", "Contacts");
  94.     }

  95.     int Insert()
  96.     {
  97.         cout << "INSERT" << endl;
  98.         int n = contactsOpr.Update(
  99.                 "INSERT INTO contacts VALUES(0, 'user1', '1390000000')");
  100.         n += contactsOpr.Update(
  101.                 "INSERT INTO contacts VALUES(0, 'user2', '1380000000')");
  102.         cout << "update " << n << endl;
  103.         return n;
  104.     }

  105.     int Delete()
  106.     {
  107.         cout << "DELETE" << endl;
  108.         int n = contactsOpr.Update(
  109.                 "DELETE FROM contacts WHERE name = 'user1'");
  110.         cout << "update " << n << endl;
  111.         return n;
  112.     }

  113.     int Update()
  114.     {
  115.         cout << "UPDATE" << endl;
  116.         int n = contactsOpr.Update(
  117.                 "UPDATE contacts SET phone = '1386666666'");
  118.         cout << "update " << n << endl;
  119.         return n;
  120.     }

  121.     int Select()
  122.     {
  123.         cout << "SELECT" << endl;
  124.         unique_ptr<sql::ResultSet> res = contactsOpr.Query(
  125.                 "SELECT * FROM contacts");
  126.         if (NULL == res.get())
  127.             return -1;
  128.         while (res->next())
  129.         {
  130.             cout << "Recode: " << res->getInt("uid") << ", "
  131.                 << res->getString("name") << ", " << res->getString("phone")
  132.                 << endl;
  133.         }
  134.         return 0;
  135.     }

  136. private:
  137.     DBOperator() {}

  138.     MySQLOpr contactsOpr;
  139. };

  140. int main()
  141. {
  142.     DBOperator::Instance().Init();
  143.     DBOperator::Instance().Insert();
  144.     DBOperator::Instance().Select();
  145.     DBOperator::Instance().Update();
  146.     DBOperator::Instance().Select();
  147.     DBOperator::Instance().Delete();
  148.     DBOperator::Instance().Select();
  149.     return 0;
  150. }

阅读(1660) | 评论(0) | 转发(0) |
0

上一篇:libev的c++使用

下一篇:非c++1x实现UniquePtr

给主人留下些什么吧!~~