Access MySQL using MySQL c api-Example

Source: Internet
Author: User

Code:

 /*  Simple C program that connects to MySQL Database Server  */  # Include <Mysql. h> # Include <Stdio. h>
Main (){ Char * Begin = " \ N + -------------- begin --------------- + \ 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 * notifies the server that the client can process multi-statement execution or storageProgram. * If client_multi_statements is set, it is set automatically. */ 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 common table Query 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 " , Row [ 0 ]);} Mysql_free_result (RES );
Char * Tell2 = " SQL store query more... \ n " ; Printf (tell2 ); // SQL stored procedure Query Char * Sql1 = " Call p_getcurrentcostpricebyusername ('client1 @ 192.168.1.122 ') " ; If (Mysql_query (Conn, sql1) {fprintf (stderr, " % S \ n " , Mysql_error (conn); exit ( 1 );}
/* * By default, a stored procedure returns multiple result sets. * use mysql_next_result to retrieve and check * The next result set. Otherwise, the "commands out of sync; * You can't run this command now" error will occur for other query statements in the stored procedure! */ 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 " , Row [ 2 ]) ;}} While (! Mysql_next_result (conn); mysql_free_result (RES );
Char * Tell3 = " SQL View query more... \ n " ; Printf (tell3 ); // SQL View query 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 " , Row [ 5 ]);} Mysql_free_result (RES );
Mysql_close (conn );
Char * End = " + -------------- End ---------------- + \ n " ; Printf (end );}

 

Compile:

 
Gcc-O sqla $ (mysql_config -- cflags) sqla. C $ (mysql_config -- libs)

 

Running result:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.