vs2022 linux c++连接mysql

https://blog.csdn.net/weixin_45419660/article/details/140751066

https://www.kdun.com/ask/1296772.html

#include <cstdio>
#include <mysql-cppconn/jdbc/mysql_connection.h>  
#include <mysql-cppconn/jdbc/mysql_driver.h>  
#include <mysql-cppconn/jdbc/cppconn/statement.h>  

using namespace sql;
using namespace std;

int main()
{
	printf("%s 向你问好!\n", "mysql_test");
	mysql::MySQL_Driver* driver;
	Connection* con;
	Statement* state;
	ResultSet* result;
	// 初始化驱动  
	driver = sql::mysql::get_mysql_driver_instance();
	// 建立链接  
	con = driver->connect("tcp://192.168.1.9:3306", "root", "123456");
	state = con->createStatement();
	state->execute("use books");
	// 查询  
	result = state->executeQuery("select * from user");
	// 输出查询  
	while (result->next())
	{
		int id = result->getInt("id");
		string name = result->getString("name");
		cout << id << " : " << name << endl;
	}

	system("read");
    
	delete state;
	delete con;
	return 0;
}

 

posted @ 2024-12-22 13:14  微笑的''80  阅读(24)  评论(0)    收藏  举报