Linux下使用C语言连接Mysql
/* Simple C program that connects to MySQL Database server*/
#include
#include
main()
{
char *begin="\n+--------------BEGIN---------------+\n\n";
printf(begin);
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = ""; /* 此处改成你的密码 */
char *database = "epude_school";
conn = mysql_init(NULL);
/* Connect to database */
/*
CLIENT_MULTI_RESULTS
通知服务器,客户端能够处理来自多语句执行或存储程序的多个结果集。
如果设置了CLIENT_MULTI_STATEMENTS,将自动设置它。
*/
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, CLIENT_MULTI_RESULTS)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
char *tell="SQL Table Query...\n";
printf(tell);
// SQL 普通表查询
char *sql="select password from Users whereUserName='client1@192.168.1.122'";
if (mysql_query(conn, sql))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("SqlCommand:%s",sql);
printf("\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("PassWord:%s \n\n", row[0]);
}
mysql_free_result(res);
char *tell2="SQL Store Query More...\n";
printf(tell2);
// SQL 存储过程查询
char *sql1="call P_GetCurrentCostPriceByUserName('client1@192.168.1.122')";
if (mysql_query(conn, sql1))
{
fprintf(stderr, "%s\n\n", mysql_error(conn));
exit(1);
}
/*
存储过程默认返回的是多个结果集,
所以要用mysql_next_result取出并检查
下一个的结果集。否则在存储过程下的
其它查询语句都会出现 “Commands out of sync;
you can't run this command now”错误!
*/
do
{
if ((res = mysql_use_result(conn)))
{
printf("SqlCommand:%s",sql1);
printf("\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("UserName:%s \n", row[0]);
printf("Balance:%s \n",row[1]);
printf("Price:%s \n\n",row[2]);
}
}
}while (!mysql_next_result(conn));
mysql_free_result(res);
char *tell3="SQL View Query More...\n";
printf(tell3);
// SQL 视图查询
char *sql2="select CameraID,URL,RtspName,PW,PTZ,PTZServer from V_UserEquipment whereLoginName='client1@192.168.1.122'";
if (mysql_query(conn, sql2))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("SqlCommand:%s",sql2);
printf("\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("CameraID:%s \n", row[0]);
printf("URL:%s\n",row[1]);
printf("RtspName:%s \n",row[2]);
printf("PW:%s \n", row[3]);
printf("PTZ:%s \n",row[4]);
printf("PTZServer:%s \n\n",row[5]);
}
mysql_free_result(res);
mysql_close(conn);
char *end="+--------------END----------------+\n";
printf(end);
}
/*编译*/
// gcc -o sqla $(mysql_config --cflags) sqla.c $(mysql_config --libs)
/*运行结果*/
+--------------BEGIN---------------+
SQL Table Query...
SqlCommand:select password from Users where UserName='client1@192.168.1.122'PassWord:client1
SQL Store Query More...
SqlCommand:call P_GetCurrentCostPriceByUserName('client1@192.168.1.122')
UserName:client1@192.168.1.122
Balance:30000
Price:0.05
SQL View Query More...
SqlCommand:select CameraID,URL,RtspName,PW,PTZ,PTZServer from V_UserEquipment where LoginName='client1@192.168.1.122'CameraID:051010049@192.168.1.122_0
[url=rtsp://192.168.1.93/1.mp4]URL:rtsp://192.168.1.93/1.mp4[/url]
RtspName:admin
PW:admin
PTZ:1
PTZServer:ptzserver1@192.168.1.122
+--------------END----------------+
©著作权归作者所有:来自51CTO博客作者bruceleeliya的原创作品,如需转载,请注明出处,否则将追究法律责任