Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <memory>
  5. #include <mysql_driver.h>
  6. #include <mysql_connection.h>
  7. #include <cppconn/driver.h>
  8. #include <cppconn/statement.h>
  9. #include <cppconn/prepared_statement.h>
  10. #include <cppconn/metadata.h>
  11. #include <cppconn/exception.h>
  12.  
  13. using namespace std;
  14. using namespace sql;
  15.  
  16. int main()
  17. {
  18. sql::mysql::MySQL_Driver *driver = 0;
  19. sql::Connection *conn = 0;
  20.  
  21. try
  22. {
  23. driver = sql::mysql::get_mysql_driver_instance();
  24. conn = driver->connect("tcp://localhost:3306/ip_store", "root", "");
  25. cout << "连接成功" << endl;
  26. }
  27. catch (...)
  28. {
  29. cout << "连接失败" << endl;
  30. }
  31. sql::Statement* stat = conn->createStatement();
  32. // stat->execute("set names 'gbk'");
  33. ResultSet *res;
  34. res = stat->executeQuery("SELECT * FROM t_ip limit 10");
  35. while (res->next())
  36. {
  37. cout << "ip_start" << res->getString("ip_start") << endl;
  38. cout << "ip_end:" << res->getString("ip_end") << endl;
  39. cout << "region:" << res->getString("region") << endl;
  40. }
  41. if (conn != 0)
  42. {
  43. delete conn;
  44. }
  45. system("pause");
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement