Ps:官方Connector C++说明截取如下
On Windows platforms, Commercial and Community Connector/C++distributions require the Visual C++ Redistributable for VisualStudio. The Redistributable is available at the Visual Studio DownloadCenter; install it before installing Connector/C++. Theacceptable Redistributable versions depend on your Connector/C++version:
•Connector/C++ 8.0.19 and higher: VC++ Redistributable 2017 orhigher.
•Connector/C++ 8.0.14 to 8.0.18: VC++ Redistributable 2015 orhigher.
#include
#include "jdbc/mysql_connection.h"
#include "jdbc/mysql_driver.h"
#include "jdbc/cppconn/statement.h"
#include "jdbc/cppconn/prepared_statement.h"
using namespace std;
using namespace sql;
int main()
{
sql::PreparedStatement* prep_stmt;
int updatecount = 0;
std::cout << "Hello World!\n";
std::cout << "test mysql\n";
sql::mysql::MySQL_Driver* driver = NULL;
sql::Connection* conn = NULL;
driver = sql::mysql::get_mysql_driver_instance();
if (driver == NULL)
{
std::cout <<"driver is null" << std::endl;
}
conn = driver->connect("tcp://localhost:3306", "root", "123456");
if (conn == NULL)
{
std::cout << "conn is null" << std::endl;
}
std::cout << "connect suceess" << std::endl;
//查询
int flag = 0;
sql::Statement* stmt = conn->createStatement();
sql::ResultSet* res;
res = stmt->executeQuery("SELECT * FROM cms_device");
while (res->next())
{
cout << res->getInt("id") << endl;
cout << res->getString("phone").c_str() << endl;
cout << res->getString("imsi").c_str() << endl;
}
//插入
conn->setAutoCommit(0);//关闭自动提交
res->first();
flag = 0;
while (res->next())
{
if (strcmp(res->getString("imsi").c_str(), "460010010000100") == 0)
{
flag = 1;
break;
}
}
if (flag == 0) {
prep_stmt = conn->prepareStatement("INSERT INTO cms_device (id,phone,imsi) VALUES (111,?,?)");
prep_stmt->setString(1, "15043214321");
prep_stmt->setString(2, "460010010000100");
updatecount = prep_stmt->executeUpdate();
}
Savepoint* savept;
savept = conn->setSavepoint("SAVEPT1");
res->first();
flag = 0;
while (res->next())
{
if (strcmp(res->getString("imsi").c_str(), "460010010000101") == 0)
{
flag = 1;
break;
}
}
if (flag == 0) {
prep_stmt = conn->prepareStatement("INSERT INTO cms_device (phone,imsi) VALUES (?,?)");
prep_stmt->setString(1, "15043214321");
prep_stmt->setString(2, "460010010000101");
updatecount = prep_stmt->executeUpdate();
}
conn->rollback(savept);
conn->releaseSavepoint(savept);
conn->commit();
//更新
conn->setAutoCommit(1);//打开自动提交
prep_stmt = conn->prepareStatement("update cms_device set phone=? where phone=?");
prep_stmt->setString(1, "15011111111");
prep_stmt->setString(2, "15043214321");
updatecount = prep_stmt->executeUpdate();
system("pause");
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)