使用 MySQL C API 訪問 MySQL — 樣本

來源:互聯網
上載者:User

代碼: 

/* 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----------------+

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.