[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?
- 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?
- 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?
- Create database foo;
Use the following SQL statement to create a table and insert data:
[Cpp]View plaincopyprint?
- Create table children (
- Childno int (11) not null auto_increment,
- Fname varchar (30 ),
- Age int (11 ),
- Primary key (childno)
- );
- Insert into children (childno, fname, age) VALUES (1, 'jenny ', 21 );
- Insert into children (childno, fname, age) VALUES (2, 'Andrew ', 17 );
- Insert into children (childno, fname, age) VALUES (3, 'gavin', 8 );
- Insert into children (childno, fname, age) VALUES (4, 'Duncan ', 6 );
- Insert into children (childno, fname, age) VALUES (5, 'emma', 4 );
- Insert into children (childno, fname, age) VALUES (6, 'Alex ', 15 );
- 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?
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include "mysql. h"
- Int main (int argc, char * argv [])
- {
- MYSQL * conn_ptr;
- Conn_ptr = mysql_init (NULL );
- If (! Conn_ptr)
- {
- Fprintf (stderr, "mysql_init failed \ n ");
- Return EXIT_FAILURE;
- }
- Conn_ptr = mysql_real_connect (conn_ptr, "localhost", "rick", "rick", "foo", 0, NULL, 0 );
- If (conn_ptr)
- Printf ("Connection success \ n ");
- Else
- Printf ("Connection failed \ n ");
- Mysql_close (conn_ptr );
- Return EXIT_SUCCESS;
- }
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?
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include "mysql. h"
- Int main ()
- {
- MYSQL my_connecyion;
- Int res;
- Mysql_init (& my_connecyion );
- If (mysql_real_connect (& my_connecyion, "localhost", "rick", "rick", "foo", 0, NULL, 0 ))
- {
- Printf ("Connection success \ n ");
- // Execute the SQL statement
- Res = mysql_query (& my_connecyion, "insert into children (fname, age) VALUES ('ann ', 3 )");
- If (! Res)
- Printf ("Inserted % lu rows \ n", (unsigned long) mysql_affected_rows (& my_connecyion ));
- Else
- Fprintf (stderr, "Insert error % d: % s \ n", mysql_errno (& my_connecyion), mysql_error (& my_connecyion ));
- Mysql_close (& my_connecyion );
- }
- Else {
- Fprintf (stderr, "Connection failed \ n ");
- If (mysql_errno (& my_connecyion ))
- Fprintf (stderr, "Connection error % d: % s \ n", mysql_errno (& my_connecyion), mysql_error (& my_connecyion ));
- }
- Return EXIT_SUCCESS;
- }
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.