Simple MySQL database access using C language
Unlike Oracle, MYSQL databases do not support embedded SQL languages.
However, it provides powerful C and C ++ support APIs. The C language API is in part 1 of the Reference Manual, and is independent of other parts except the reference manual of more than 25th pages.
MySQL ++ is a MySQL API for C ++. Warren Young is responsible for the project. For more information, visit http://www.mysql.com/products/mysql /.
Of course, mysql seems to be the most widely used php scripting language, apache, used together as the website's back-end database service program, this has done a lot.
At the same time, I am a C programmer. I always want to try something with c. I have been busy preparing for the military transfer exam these days,
Just a little spare time. By the way, I will review the access to the c and mysql databases.
The runtime environment is SunOS x4100 5.10 Generic_118855-33 i86pc i386 i86pc; the compiler is gcc, plus Llibmysqlclient library.
- # Include<Stdio. h>
- # Include<String. h>
- # Include<Mysql. h>
- # Include<Stdlib. h>
- Main ()
- {
- MYSQL * conn;
- MYSQL_RES * res;
- MYSQL_ROW row;
- Char *Server="Mysql";
- Char *User="Huzia";
- Char *Password="CjrIwo";/**/
- Char *Database="Huzia";
- 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 ));
- Exit (1 );
- }
- /* Send SQL query */
- If (mysql_query (conn, "show tables ")){
- Fprintf (stderr, "% s \ n", mysql_error (conn ));
- Exit (1 );
- }
- Res=Mysql_use_result(Conn );
- /* Output table name */
- Printf ("MySQL Tables in mysql database: \ n ");
- While ((Row=Mysql_fetch_row(Res ))! = NULL)
- Printf ("% s \ n", row [0]);
- /* Close connection */
- Mysql_free_result (res );
- Mysql_close (conn );
- }
Run successfully.