关于准备工作,请看上篇文章https://blog.csdn.net/wjl990316fddwjl/article/details/135357755
/数据库连接,先不考虑线程安全
//unsigned short port 65535
//flag可以设置支持多条语句 默认为0;port=3306
bool Connect(const char*host,const char*user,const char* password,const char*database,unsigned short port=3306,unsigned short flag=0);
bool LXMysql::Connect(const char* host, const char* user, const char* password, const char* database, unsigned short port, unsigned short flag)
{
//if (!Init())//防止内存泄露
//{
// cerr << "Connect failed,mysql is not init!" << endl;
// return false;
//}
if (!mysql && Init())//防止内存泄露 这样写是因为写了超时那个
{
cerr << "Connect failed,mysql is not init!" << endl;
return false;
}
if (!mysql_real_connect(mysql, host, user, password, database, port, 0, NULL))
{
cerr << "mysql_real_connect failed!" << mysql_error(mysql) << endl;
return false;
}
cout << "mysql connect success!" << endl;
return true;
}
在test_LXMysql.cpp里面写
//2、mysql连接,如果没有调用init,内部会自动调用
if (my.Connect("127.0.0.1", "root", "990107Wjl@", "database_test"))
{
cout << "my.connect success!" << endl;
}