Getting Started with the "Linux" Ubuntu C language Access MySQL database

Source: Internet
Author: User
Tags mysql command line

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?
    1. 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?
    1. 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?
    1. CREATE DATABASE foo;

Then use the following SQL statement to create the table and insert the data:

[CPP]View Plaincopyprint?
  1. CREATE TABLE Children (
  2. Childno Int (one) not NULL auto_increment,
  3. fname varchar (30),
  4. Age Int (one),
  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,' 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?
  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 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?
  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 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. }


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

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.