用C代码连接mysql,网上看到的例子,代码如下#include <stdio.h>
#include <stdlib.h>#include "mysql.h"
#include <windows.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()
{
printf("Hello world!\n"); 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 = "user"; // the root user of mysql
mysqlD.password = "pws"; // the password of the root user in mysql
mysqlD.database = "database"; // 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;
}当我编译的时候出现如下错误,
\include\mysql_com.h|268|error: expected specifier-qualifier-list before 'SOCKET'|
\include\mysql_com.h|437|error: expected ')' before 's'|在网上看到一个帖子说是缺少“__LCC__”这个定义变量,可以在程序中自己定义,或者加载“winsock.h”库文件或"windows.h"库文件。我都试过,没有成功。。
帖子∶http://www.huomo.cn/database/article-ce00.html求高手帮忙
#include <stdlib.h>#include "mysql.h"
#include <windows.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()
{
printf("Hello world!\n"); 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 = "user"; // the root user of mysql
mysqlD.password = "pws"; // the password of the root user in mysql
mysqlD.database = "database"; // 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;
}当我编译的时候出现如下错误,
\include\mysql_com.h|268|error: expected specifier-qualifier-list before 'SOCKET'|
\include\mysql_com.h|437|error: expected ')' before 's'|在网上看到一个帖子说是缺少“__LCC__”这个定义变量,可以在程序中自己定义,或者加载“winsock.h”库文件或"windows.h"库文件。我都试过,没有成功。。
帖子∶http://www.huomo.cn/database/article-ce00.html求高手帮忙
解决方案 »
- 问个简单问题,最近看了几篇牛人写的文章,好像都不推荐使用连接和外键?这是为什么
- 有没有能让某个字段自增的SQL文
- 求教,关于数据库的乱码问题
- mysql插入记录500条可以,插入1000条就报错。为什么?
- 诡异的group by ????????????大家都来看看
- MySQL Select 语句返回记录集数量-1?
- 每天1万的数据要添加或者更新,mysql行吗
- mysql 批量 update 时碰到的奇怪问题。。。
- 玩过Postgresql的高手进来帮一下忙!
- 急!!!如何让mysql-4.0.12不启用innodb
- C代码连接mysql问题
- mysql发生诡异事件,有个正常字母的用户名无法查询
#include <windows.h>
把这个头文件放到最前边。
我把它放在最前面试了一下,出现了50个错误和100多warining。。貌似还是不行~
我用的是codeblock, vc没用过,可以问问具体怎么做么?