[Linux] Ubuntu C language access MySQL database entry, tumysql

Source: Internet
Author: User

[Linux] Ubuntu C language access MySQL database entry, tumysql

The system used is Ubuntu 11. 10. The database is MySQL.

 

MySQL database environment Configuration

 

First, install the MySQL client and server. The command line installation method is as follows:

 

[Cpp]View plaincopyprint?
  1. Sudo apt-get install mysql-server mysql-client


Then, to access the database using C programming language, you need to install another development kit:

 

 

[Cpp]View plaincopyprint?
  1. Sudo apt-get install libmysqlclient15-dev

 

 

Create a database in MySQL

First, log on to the MySQL database with user rick (the user rick has been granted the root permission to create a database and so on ):

Create a database named foo:

 

[Cpp]View plaincopyprint?
  1. Create database foo;

Use the following SQL statement to create a table and insert data:

 

 

[Cpp]View plaincopyprint?
  1. Create table children (
  2. Childno int (11) not null auto_increment,
  3. Fname varchar (30 ),
  4. Age int (11 ),
  5. Primary key (childno)
  6. );
  7. Insert into children (childno, fname, age) VALUES (1, 'jenny ', 21 );
  8. Insert into children (childno, fname, age) VALUES (2, 'Andrew ', 17 );
  9. Insert into children (childno, fname, age) VALUES (3, 'gavin', 8 );
  10. Insert into children (childno, fname, age) VALUES (4, 'Duncan ', 6 );
  11. Insert into children (childno, fname, age) VALUES (5, 'emma', 4 );
  12. Insert into children (childno, fname, age) VALUES (6, 'Alex ', 15 );
  13. Insert into children (childno, fname, age) VALUES (7, 'filena', 9 );


Run the following command in MySQL Command Line Mode:

 

 

MySQL database connection test

 

Then use the following C language for database connection test connect1.c:

 

[Cpp]View plaincopyprint?
  1. # Include <stdlib. h>
  2. # Include <stdio. h>
  3. # Include "mysql. h"
  4. Int main (int argc, char * argv [])
  5. {
  6. MYSQL * conn_ptr;
  7. Conn_ptr = mysql_init (NULL );
  8. If (! Conn_ptr)
  9. {
  10. Fprintf (stderr, "mysql_init failed \ n ");
  11. Return EXIT_FAILURE;
  12. }
  13. Conn_ptr = mysql_real_connect (conn_ptr, "localhost", "rick", "rick", "foo", 0, NULL, 0 );
  14. If (conn_ptr)
  15. Printf ("Connection success \ n ");
  16. Else
  17. Printf ("Connection failed \ n ");
  18. Mysql_close (conn_ptr );
  19. Return EXIT_SUCCESS;
  20. }

Execution result:

 

Note: You need to specify the path name of the include library and library file, and the library module mysqlclient that specifies the link.

If you do not install the SDK at the beginning, the following error occurs:

 

Execute SQL statements for Data Operations

Insert a row to the children database table:

 

[Cpp]View plaincopyprint?
  1. # Include <stdlib. h>
  2. # Include <stdio. h>
  3. # Include "mysql. h"
  4. Int main ()
  5. {
  6. MYSQL my_connecyion;
  7. Int res;
  8. Mysql_init (& my_connecyion );
  9. If (mysql_real_connect (& my_connecyion, "localhost", "rick", "rick", "foo", 0, NULL, 0 ))
  10. {
  11. Printf ("Connection success \ n ");
  12. // Execute the SQL statement
  13. Res = mysql_query (& my_connecyion, "insert into children (fname, age) VALUES ('ann ', 3 )");
  14. If (! Res)
  15. Printf ("Inserted % lu rows \ n", (unsigned long) mysql_affected_rows (& my_connecyion ));
  16. Else
  17. Fprintf (stderr, "Insert error % d: % s \ n", mysql_errno (& my_connecyion), mysql_error (& my_connecyion ));
  18. Mysql_close (& my_connecyion );
  19. }
  20. Else {
  21. Fprintf (stderr, "Connection failed \ n ");
  22. If (mysql_errno (& my_connecyion ))
  23. Fprintf (stderr, "Connection error % d: % s \ n", mysql_errno (& my_connecyion), mysql_error (& my_connecyion ));
  24. }
  25. Return EXIT_SUCCESS;
  26. }


Running result:

 

 

Note the following:

The mysql_affected_rows function returns the number of rows modified by an update operation, rather than the number of rows that meet the where clause.

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.