//linux下编译g++ $(mysql_config --cflags) ***.cpp $(mysql_config --libs)
/*mysql数据库中表的内容
mysql> select * from maindb;
+------------------+---------+-------------+---------+----------+---------------------+---------------------+---------------------+--------------------+
| id | leixing | wenjianming | zhanhao | zhanming | scsj | clsj | sxsj | baowen |
+------------------+---------+-------------+---------+----------+---------------------+---------------------+---------------------+--------------------+
| 0000000000003215 | sp | sp321654 | 12 | fuzhou | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | oiyhjkghjdgfskdjfh |
| 0000000000003216 | NULL | pet.txt | 256 | 福州 | 1970-01-01 08:04:16 | NULL | 1970-01-01 08:09:08 |$
|
| 0000000000003217 | NULL | pet.txt | 256 | 福州 | 1970-01-01 08:04:16 | NULL | 1970-01-01 08:09:08 |$
|
+------------------+---------+-------------+---------+----------+---------------------+---------------------+---------------------+--------------------+
3 rows in set (0.00 sec)
*/
#include <mysql/mysql.h>
#include <stdio.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";/*服务器名*/
char *user = "root";/*用户名*/
char *password = "123"; /* 此处改成你的密码 */
char *database = "mysql";/*数据库名*/
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s/n", mysql_error(conn));
return 1;
}
/* send SQL query */
if (mysql_query(conn, "select * from maindb where zhanhao=256")) {//sql语句
fprintf(stderr, "%s/n", mysql_error(conn));
return 1;
}
res = mysql_use_result(conn);//保存查询结果
/* output */
while ((row = mysql_fetch_row(res)) != NULL)
{
for(int i=0;i<9;i++)
printf("%s ", row[i]);
printf("/n");
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
C语言连接mysql数据库,读取mysql内容
最新推荐文章于 2023-03-20 22:27:17 发布