2)建立连接
所用到的函数如下:
MYSQL *mysql_init(MYSQL *connection) // 初始化连接句柄
//成功返回MySQL结构指针,失败返回NULL
MYSQL *mysql_real_connect(MYSQL *connection,
const char *server_host,
const char *sql_user_name,
const char *sql_password,
const char *db_name,
unsigned int port_number,
const char *unix_socket_name,
unsigned int flags) //建立连接
//成功返回MySQL结构指针,失败返回NULL
以下是完整实例:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <mysql/mysql.h>
using namespace std
void mysql_err_function(MYSQL * connection)
int main()
{
//freopen("input.txt","r",stdin)
MYSQL * connection
connection = mysql_init(NULL)
if (!connection)
{
cout <<"mysql_init failed!" <<endl
exit(-1)
}
if (!mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))
{
cout <<"Connection To MySQL failed!" <<endl
mysql_err_function(connection)
}
cout <<"Connection To MySQL Server is Success..." <<endl
string str
getline(cin,str)
int res = 0
int affected_count = 0
while (str != "close" &&str != "" &&!res)
{
res = mysql_query(connection,str.c_str())
affected_count += mysql_affected_rows(connection)
if (res)
{
if (mysql_errno(connection))
{
cout <<"Error " <<mysql_errno(connection) <<" : "
<<mysql_error(connection) <<'\n' <<endl
break
}
}
getline(cin,str)
}
cout <<"Have affected " <<affected_count <<" rows!" <<endl
mysql_close(connection)
cout <<"Connection To MySQL Server is closed..." <<endl
return 0
}
void mysql_err_function(MYSQL * connection)
{
if (mysql_errno(connection))
{
cout <<"Error " <<mysql_errno(connection) <<" : "
<<mysql_error(connection) <<endl
exit(-1)
}
}
只要libmysqlclient.so在/usr/lib/mysql 中,-L/usr/lib/mysql -lmysqlclient 就是link libmysqlclient.so所以,你应该找一下libmysqlclient.so的安装位置,比如安装在/usr/lib/i386-linux-gnu
链接libmysqlclient.so,就是-L/usr/lib/i386-linux-gnu -lmysqlclient
另外,这种编译出现的问题,请贴出具体的输出,不要泛泛的讲。泛泛的讲别人根本不明白是那里出了问题。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)