#ifndef _CONNET_SQL_
#define _CONNET_SQL_
#include
#include
#include
#include
#include
#include
#include
#include "mysql_connection.h"
#include
#include
#include
#include
using std::string;
using std::vector;
using std::cout;
using std::endl;
using std::getline;
using std::atoi;
using std::to_string;
using std::ofstream;
using std::fstream;
using Eigen::Matrix;
using Eigen::Dynamic;
class ConnetSQL
{
public:
ConnetSQL(string location, string dbName, string passWord);
~ConnetSQL() {
delete driver;
delete connection;
}
void createSchema(string name); //schema 是否存在
void showDatabases();
void ceateTable(string schemaName, string tableName);
void showTables(string scheamName);
void pushMatrix(string scheamName,vector>& data,string tableName); //默认名字0开始
private:
void saveTrainData(string path, string scheamName);
vector split(string str, string pattern);
bool isExist(string name);
bool isExistTable(string scheamName, string tablename); //table是否存在
private:
sql::mysql::MySQL_Driver* driver=0;
sql::Connection* connection=0;
};
//第一次 连接 创数据库 建表导入数据
//第二次 连接 导出数据 或者 新建表存数据
#endif // !_CONNET_SQL_
#include"connetSQL.h"
ConnetSQL::ConnetSQL(string location, string adName, string passWord) {
cout << "连接数据库中---------------------" << endl;
try {
driver = sql::mysql::get_mysql_driver_instance();
connection = driver->connect(location, adName, passWord);
cout << "连接成功" << endl;
}
catch(...) {
cout << "连接失败" << endl;
}
}
void ConnetSQL::createSchema(string name) {
if (isExist(name))
cout << "schema已存在" << endl;
else {
sql::Statement* state = connection->createStatement();
string str = "CREATE DATABASE "+ name;
state->execute(str);
}
}
bool ConnetSQL::isExist(string name) {
sql::ResultSet* res;
sql::Statement* state = connection->createStatement();
res = state->executeQuery("SHOW DATABASES");
while (res->next()) {
if (name == res->getString(1))
return true;
}
return false;
}
void ConnetSQL::showDatabases() {
sql::Statement* state = connection->createStatement();
sql::ResultSet* res = state->executeQuery("SHOW DATABASES");
while (res->next())
cout << res->getString(1)<setSchema(scheamName);
sql::Statement* state = connection->createStatement();
sql::ResultSet* res = state->executeQuery("SHOW TABLES");
while (res->next()) {
if (res->getString(1) == name)
return true;
}
return false;
}
void ConnetSQL::ceateTable(string scheamName, string tableName) {
if (isExist(scheamName)) {
if (isExistTable(scheamName,tableName))
cout << "表已存在" << endl;
else {
connection->setSchema(scheamName);
sql::Statement* state = connection->createStatement();
string str = "CREATE TABLE " + tableName+" (name varchar(10))";
state->execute(str);
return;
}
}
cout << "scheame不存在" << endl;
}
void ConnetSQL::showTables(string scheamName) {
connection->setSchema(scheamName);
sql::Statement* state = connection->createStatement();
sql::ResultSet* res;
res = state->executeQuery("SHOW TABLES");
while (res->next()) {
cout << res->getString(1) << endl;
}
}
void ConnetSQL::saveTrainData(string path, string scheamName) {
fstream in;
in.open(path);
connection->setSchema(scheamName);
sql::Statement* state = connection->createStatement();
state->execute("CREATE TABLE t_trainData(id int primary key auto_increment,x1 double,y1 double,x2 double,y2 double,x3 double,y3 double,x4 double,y4 double,x5 double,y5 double,x6 double,y6 double,x7 double,y7 double)");
while (!in.eof()) {
string test;
getline(in, test);
vector vec = split(test, " ");
vec[0] = to_string(0);
string str = "INSERT INTO t_trainData(id,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7) VALUES(" + vec[0] + "," + vec[1] + "," + vec[2] + "," + vec[3] + "," + vec[4] + "," + vec[5] + "," + vec[6] + "," + vec[7] + "," + vec[8] + "," + vec[9] + "," + vec[10] + "," + vec[11] + "," + vec[12] + "," + vec[13] +","+vec[14]+ ")";
state->execute(str);
}
cout << "结束" << endl;
}
vector ConnetSQL::split(string str, string pattern) {
string::size_type pos;
vector result;
str += pattern;
for (int i = 0; i < str.size(); i++) {
pos = str.find(pattern, i);
if (pos>& data, string tableName) {
}