代碼:
/* Simple C program that connects to MySQL Database server */ #include <mysql.h> #include <stdio.h>
main() { char *begin="\n+--------------BEGIN---------------+\n\n"; printf(begin);
MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "your mysql user"; char *password = "your password"; char *database = "your database";
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----------------+