The system used is Ubuntu 11.10. The database is MySQL.
MySQL Database environment configuration
You first need to install the MySQL client and server, the command line installation method is:
[CPP]View Plaincopyprint?
- sudo apt-get install mysql-server mysql-client
Then, to access the database using C programming, you need to install a separate development package:
[CPP]View Plaincopyprint?
- sudo apt-get install Libmysqlclient15-dev
Build the database in MySQL
First log in to the MySQL database with user Rick (user Rick has been given permission to create the database and so on by the root user):
Then create a database named Foo:
[CPP]View Plaincopyprint?
- CREATE DATABASE foo;
Then use the following SQL statement to create the table and insert the data:
[CPP]View Plaincopyprint?
- CREATE TABLE Children (
- Childno Int (one) not NULL auto_increment,
- fname varchar (30),
- Age Int (one),
- 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,' Adrian ', 9);
The following methods are executed 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 that 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 development package at the beginning, the following error is generated:
Execute SQL statement for data operation
Insert a row into the database table children:
[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 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;
- }
Operation Result:
In particular, it is important to note that:
The function Mysql_affected_rows returns the number of rows modified by an update operation, rather than the number of rows that satisfy the WHERE clause.
Getting Started with the "Linux" Ubuntu C language Access MySQL database