Simple MySQL database access using C language

Source: Internet
Author: User

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.

  1. # Include<Stdio. h>
  2. # Include<String. h>
  3. # Include<Mysql. h>
  4. # Include<Stdlib. h>
  5. Main ()
  6. {
  7. MYSQL * conn;
  8. MYSQL_RES * res;
  9. MYSQL_ROW row;
  10. Char *Server="Mysql";
  11. Char *User="Huzia";
  12. Char *Password="CjrIwo";/**/
  13. Char *Database="Huzia";
  14. Conn=Mysql_init(NULL );
  15. /* Connect to database */
  16. If (! Mysql_real_connect (conn, server,
  17. User, password, database, 0, NULL, 0 )){
  18. Fprintf (stderr, "% s \ n", mysql_error (conn ));
  19. Exit (1 );
  20. }
  21. /* Send SQL query */
  22. If (mysql_query (conn, "show tables ")){
  23. Fprintf (stderr, "% s \ n", mysql_error (conn ));
  24. Exit (1 );
  25. }
  26. Res=Mysql_use_result(Conn );
  27. /* Output table name */
  28. Printf ("MySQL Tables in mysql database: \ n ");
  29. While ((Row=Mysql_fetch_row(Res ))! = NULL)
  30. Printf ("% s \ n", row [0]);
  31. /* Close connection */
  32. Mysql_free_result (res );
  33. Mysql_close (conn );
  34. }

Run successfully.

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.