MySQL使用C++連結語法
連線、建立資料表及插入資料
Update the relevant include and lib directories in Visual Studio Community 2017
a. C:\Program Files\MySQL\Connector C++ 1.1\include
b. C:\Program Files\MySQL\MySQL Server 8.0\include
c. C:\Program Files\MySQL\Connector C++ 1.1\lib\opt
d. C:\Program Files\MySQL\MySQL Server 8.0\lib
e. mysqlcppconn.lib
f. libmysql.lib
#include <stdlib.h> #include <iostream> #include "stdafx.h" #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/prepared_statement.h> using namespace std; //for demonstration only. never save your password in the code! const string server = "tcp://yourservername.mysql.database.azure.com:3306"; const string username = "username@servername"; const string password = "yourpassword"; int main() { sql::Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::PreparedStatement *pstmt; try { driver = get_driver_instance(); con = driver->connect(server, username, password); } catch (sql::SQLException e) { cout << "Could not connect to server. Error message: " << e.what() << endl; system("pause"); exit(1); } //please create database "quickstartdb" ahead of time con->setSchema("quickstartdb"); stmt = con->createStatement(); stmt->execute("DROP TABLE IF EXISTS inventory"); cout << "Finished dropping table (if existed)" << endl; stmt->execute("CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);"); cout << "Finished creating table" << endl; delete stmt; pstmt = con->prepareStatement("INSERT INTO inventory(name, quantity) VALUES(?,?)"); pstmt->setString(1, "banana"); pstmt->setInt(2, 150); pstmt->execute(); cout << "One row inserted." << endl; pstmt->setString(1, "orange"); pstmt->setInt(2, 154); pstmt->execute(); cout << "One row inserted." << endl; pstmt->setString(1, "apple"); pstmt->setInt(2, 100); pstmt->execute(); cout << "One row inserted." << endl; delete pstmt; delete con; system("pause"); return 0; }
讀取資料
#include <stdlib.h> #include <iostream> #include "stdafx.h" #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/prepared_statement.h> using namespace std; //for demonstration only. never save your password in the code! const string server = "tcp://yourservername.mysql.database.azure.com:3306"; const string username = "username@servername"; const string password = "yourpassword"; int main() { sql::Driver *driver; sql::Connection *con; sql::PreparedStatement *pstmt; sql::ResultSet *result; try { driver = get_driver_instance(); //for demonstration only. never save password in the code! con = driver->connect(server, username, password); } catch (sql::SQLException e) { cout << "Could not connect to server. Error message: " << e.what() << endl; system("pause"); exit(1); } con->setSchema("quickstartdb"); //select pstmt = con->prepareStatement("SELECT * FROM inventory;"); result = pstmt->executeQuery(); while (result->next()) printf("Reading from table=(%d, %s, %d)\n", result->getInt(1), result->getString(2).c_str(), result->getInt(3)); delete result; delete pstmt; delete con; system("pause"); return 0; }
更新資料
#include <stdlib.h> #include <iostream> #include "stdafx.h" #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/prepared_statement.h> using namespace std; //for demonstration only. never save your password in the code! const string server = "tcp://yourservername.mysql.database.azure.com:3306"; const string username = "username@servername"; const string password = "yourpassword"; int main() { sql::Driver *driver; sql::Connection *con; sql::PreparedStatement *pstmt; try { driver = get_driver_instance(); //for demonstration only. never save password in the code! con = driver->connect(server, username, password); } catch (sql::SQLException e) { cout << "Could not connect to server. Error message: " << e.what() << endl; system("pause"); exit(1); } con->setSchema("quickstartdb"); //update pstmt = con->prepareStatement("UPDATE inventory SET quantity = ? WHERE name = ?"); pstmt->setInt(1, 200); pstmt->setString(2, "banana"); pstmt->executeQuery(); printf("Row updated\n"); delete con; delete pstmt; system("pause"); return 0; }
刪除資料
#include <stdlib.h> #include <iostream> #include "stdafx.h" #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/prepared_statement.h> using namespace std; //for demonstration only. never save your password in the code! const string server = "tcp://yourservername.mysql.database.azure.com:3306"; const string username = "username@servername"; const string password = "yourpassword"; int main() { sql::Driver *driver; sql::Connection *con; sql::PreparedStatement *pstmt; sql::ResultSet *result; try { driver = get_driver_instance(); //for demonstration only. never save password in the code! con = driver->connect(server, username, password); } catch (sql::SQLException e) { cout << "Could not connect to server. Error message: " << e.what() << endl; system("pause"); exit(1); } con->setSchema("quickstartdb"); //delete pstmt = con->prepareStatement("DELETE FROM inventory WHERE name = ?"); pstmt->setString(1, "orange"); result = pstmt->executeQuery(); printf("Row deleted\n"); delete pstmt; delete con; delete result; system("pause"); return 0; }
熱門評論