| | |
| | | #include <iostream> |
| | | #include <mysql++.h> |
| | | #include <AppConfig.h> |
| | | #include <QtCore/QMutex> |
| | | |
| | | using namespace std; |
| | | |
| | | static mysqlpp::Connection *conn; |
| | | |
| | | class BaseDao { |
| | | public: |
| | | static QMutex m_mutexVisit; |
| | | private: |
| | | static mysqlpp::Connection *conn; |
| | | public: |
| | | static mysqlpp::SimpleResult add(std::map<std::string, std::string> &columns, string tableName) { |
| | | initConnection(); |
| | |
| | | return rowDataList; |
| | | } |
| | | |
| | | static Json::Value findJsonArray(string sql) { |
| | | static Json::Value findJsonArray(string sql, std::map<std::string, std::string> &whereColumns) { |
| | | sql = sql + getWhereColumnNameValuePair(whereColumns); |
| | | initConnection(); |
| | | // conn->query("SET character_set_server = utf8;"); |
| | | mysqlpp::Query query = conn->query(sql); |
| | | std::cout << sql << std::endl; |
| | | Json::Value rowList; |
| | | if (auto res = query.store()) { |
| | | for (auto it = res.begin(); it != res.end(); ++it) { |
| | |
| | | columnValue = ""; |
| | | } |
| | | string columnName = fieldNames[0].at(i); |
| | | // if (columnName == "Nickname") { |
| | | // columnValue = columnValue;//.substr(0, 20); |
| | | // } |
| | | row[columnName] = columnValue; |
| | | } |
| | | rowList.append(row); |
| | | } |
| | | } else { |
| | | cout << "error " << query.error() << endl; |
| | | cout << "query failed" << endl; |
| | | } |
| | | return rowList; |
| | |
| | | } |
| | | |
| | | static void doConnect() { |
| | | cout << "db_host=" << appConfig.getStringProperty("db_host").c_str() << endl; |
| | | conn->set_option(new mysqlpp::SetCharsetNameOption("utf8")); |
| | | if (conn->connect( |
| | | appConfig.getStringProperty("database").c_str(), |
| | | appConfig.getStringProperty("db_host").c_str(), |
| | |
| | | // 3306 |
| | | // )) { |
| | | cout << "connect success" << endl; |
| | | mysqlpp::Query query = conn->query("SET NAMES UTF8-"); |
| | | } else { |
| | | cout << "connect failed" << endl; |
| | | } |
| | |
| | | if (!inited) { |
| | | inited = true; |
| | | conn = new mysqlpp::Connection(false); |
| | | |
| | | doConnect(); |
| | | } |
| | | if (!conn->connected() || !conn->ping()) { |