通过C++连接到mySQL
Connecting to mySQL through C++
我已经使用安装了mySQL
sudo apt-get install mySQL-server
然后我使用安装了libmysqlclient15dev
sudo apt-get install libmysqlclient15-dev
此外,我还使用安装了libmysqlc++-dev
sudo apt-get install 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 : %sn", 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 : %sn", 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("%sn", row[0]);
// clean up the database result set
mysql_free_result(res);
// clean up the database link
mysql_close(conn);
return 0;
}
但我得到了以下错误:
r@r-desktop:~/mysqlC++$ g++ test.c -I/usr/include/mysql -I/usr/include/mysql++test.c: In function ‘int main()’:
test.c:47:19: warning: deprecated conversion from string constant to ‘char*’
test.c:48:17: warning: deprecated conversion from string constant to ‘char*’
test.c:49:21: warning: deprecated conversion from string constant to ‘char*’
test.c:50:21: warning: deprecated conversion from string constant to ‘char*’
test.c:56:48: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccHFL1M4.o: In function `mysql_connection_setup(connection_details)':
test.c:(.text+0xf): undefined reference to `mysql_init'
test.c:(.text+0x51): undefined reference to `mysql_real_connect'
test.c:(.text+0x65): undefined reference to `mysql_error'
/tmp/ccHFL1M4.o: In function `mysql_perform_query(st_mysql*, char*)':
test.c:(.text+0xa2): undefined reference to `mysql_query'
test.c:(.text+0xb6): undefined reference to `mysql_error'
test.c:(.text+0xdd): undefined reference to `mysql_use_result'
/tmp/ccHFL1M4.o: In function `main':
test.c:(.text+0x170): undefined reference to `mysql_fetch_row'
test.c:(.text+0x18c): undefined reference to `mysql_free_result'
test.c:(.text+0x198): undefined reference to `mysql_close'
collect2: ld returned 1 exit status
我相信我做的每件事都是对的。。你们能指出我错在哪里吗
您还必须实际链接库(-I
只是指定include目录)。
尝试
g++ -I/usr/include/mysql -I/usr/include/mysql++ -L/usr/local/lib -lmysqlpp -lmysqlclient test.c
这是一个示例makefile。
以下是单独编译步骤的示例命令行(首先创建对象文件,然后将它们链接在一起):
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
使用此命令编译
gcc -o test -L/usr/lib/mysql -lmysqlclient test.c
源
您包含了mysql++.h
,但没有使用它。
我把你的代码改了一点
#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)
看来你在错误陈述中有拼写错误。这证明了它是你的代码。
相关文章:
- 无法在C++中建立与MySQL数据库的连接
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- MySQL 连接器/C++.尝试在此过程中停止连接到数据库
- 数据库连接并将数据从C ++程序保存到MySQL
- 为什么mysql拒绝本地连接
- 我不能使用 mysql 数据库连接同时使用插入和选择.C++
- 如何将 Unicode 字符串连接成字符串以传递到 mysql 调用中
- 如何在C API中检查MySql连接是否处于活动状态
- MYSQL连接太多错误不会消失
- 我可以在nginx + spawn-fcgi中永远打开MySQL连接吗?
- 在没有 MySql 连接器的情况下建立 MySQL 连接以进行C++
- 将 c++ 程序与 mysql 连接时出现意外错误
- 在面向对象的程序中实现MySQL连接C++
- 长时间使用活动的MySQL连接很糟糕
- qt MYSQL 连接在 Windows 上不起作用
- 在c++中关闭mysql连接时出现分段错误.如何解决
- 从不同的c++类访问Mysql*连接变量
- 用于检查MySQL连接和InnoDB插件的C/ c++程序
- MySQL连接函数崩溃
- c++ mysql连接bad_alloc使用c++连接器