C++ 通过C+连接到mySQL+;
我已经使用C++ 通过C+连接到mySQL+;,c++,mysql,linux,C++,Mysql,Linux,我已经使用 sudo apt get安装mySQL服务器 然后,我使用 sudo apt get安装libmysqlclient15 dev 此外,我还安装了libmysqlc++-dev,使用 sudo apt get安装libmysqlc++-dev 在所有这些之后,我尝试使用 g++test.c-I/usr/include/mysql-I/usr/include/mysql++ #include <mysql.h> #include <stdio.h> #inclu
sudo apt get安装mySQL服务器
然后,我使用
sudo apt get安装libmysqlclient15 dev
此外,我还安装了libmysqlc++-dev,使用
sudo apt get安装libmysqlc++-dev
在所有这些之后,我尝试使用
g++test.c-I/usr/include/mysql-I/usr/include/mysql++
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <mysql++.h>
// just going to input the general details and not the port numbers
struct connection_details
{
char *server;
char *user;
char *password;
char *database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
// first of all create a mysql instance and initialize the variables within
MYSQL *connection = mysql_init(NULL);
// connect to the database with the details attached.
if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
printf("Conection error : %s\n", mysql_error(connection));
exit(1);
}
return connection;
}
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
// send the query to the database
if (mysql_query(connection, sql_query))
{
printf("MySQL query error : %s\n", mysql_error(connection));
exit(1);
}
return mysql_use_result(connection);
}
int main()
{
MYSQL *conn; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
struct connection_details mysqlD;
mysqlD.server = "localhost"; // where the mysql database is
mysqlD.user = "root"; // the root user of mysql
mysqlD.password = "123"; // the password of the root user in mysql
mysqlD.database = "mysql"; // the databse to pick
// connect to the mysql database
conn = mysql_connection_setup(mysqlD);
// assign the results return to the MYSQL_RES pointer
res = mysql_perform_query(conn, "show tables");
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) !=NULL)
printf("%s\n", row[0]);
// clean up the database result set
mysql_free_result(res);
// clean up the database link
mysql_close(conn);
return 0;
}
我相信我做的每件事都是对的。。你们能指出我错在哪里吗?用这个命令编译
gcc -o test -L/usr/lib/mysql -lmysqlclient test.c
您还必须实际链接库(
-I
只指定包含目录)
试一试
下面是用于单独编译步骤的示例命令行(首先创建对象文件,然后将它们链接在一起):
您正在包括
mysql++.h
,但没有使用它
我把你的代码改了一点
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
/* #include <mysql++.h> Do not need this */
它运行
$ ./a.out
Conection error : Access denied for user 'root'@'localhost' (using password: YES)
看来你的错误陈述中有拼写错误。这就证明了这是您的代码。更好的办法是,将编译和链接阶段分开,但这样应该可以启动操作+1.
g++ -I/usr/include/mysql -I/usr/include/mysql++ -o test.o -c test.c
g++ -L/usr/local/lib -lmysqlpp -lmysqlclient -o test test.o
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
/* #include <mysql++.h> Do not need this */
gcc mysql-test.c $(mysql_config --cflags) $(mysql_config --libs) -Wall
$ ./a.out
Conection error : Access denied for user 'root'@'localhost' (using password: YES)