[Linux] Getting started with C language accessing MySQL database under Ubuntu

Original address: https://blog.csdn.net/xiajun07061225/article/details/8505987

related articles

1. Remotely access the mysql database under ubuntu- https://blog.csdn.net/toby54king/article/details/80781762

2. Remote access to MySQL database under Ubuntu ---- https://www.cnblogs.com/wdpp/archive/2011/11/16/2386721.html

3. Install mysql database on Ubuntu---- https://jingyan.baidu.com/article/20b68a88e0dfad796dec6243.html

4. Install MySQL and simple operation under Ubuntu---- https://www.linuxidc.com/linux/2016-07/133128.htm

5、Mysql C API编程指南----https://blog.csdn.net/superhua_nic/article/details/7401596?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.control

The system used is Ubuntu 11.10. The database is MySQL.

 

MySQL database environment configuration

 

First, you need to install the MySQL client and server. The command line installation method is:

 

sudo apt-get install mysql-server mysql-client


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

 

 

sudo apt-get install libmysqlclient15-dev

 

 

Create the corresponding database in MySQL

First, log in to the MySQL database as user rick (user rick has been granted the permissions to create databases, etc. by the root user):

Then create a database named foo:

 

CREATE DATABASE foo;

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

 

 

 
  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.  
  8. INSERT INTO children(childno,fname,age) VALUES(1,'Jenny',21);

  9. INSERT INTO children(childno,fname,age) VALUES(2,'Andrew',17);

  10. INSERT INTO children(childno,fname,age) VALUES(3,'Gavin',8);

  11. INSERT INTO children(childno,fname,age) VALUES(4,'Duncan',6);

  12. INSERT INTO children(childno,fname,age) VALUES(5,'Emma',4);

  13. INSERT INTO children(childno,fname,age) VALUES(6,'Alex',15);

  14. INSERT INTO children(childno,fname,age) VALUES(7,'Adrian',9);


The execution method in MySQL command line mode is as follows:

 

 

MySQL database connection test

 

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

 

 
  1. #include <stdlib.h>

  2. #include <stdio.h>

  3.  
  4. #include "mysql.h"

  5.  
  6. int main(int argc,char *argv[])

  7. {

  8. MYSQL *conn_ptr;

  9. conn_ptr = mysql_init(NULL);

  10.  
  11. if(!conn_ptr)

  12. {

  13. fprintf(stderr,"mysql_init failed\n");

  14. return EXIT_FAILURE;

  15. }

  16.  
  17. conn_ptr = mysql_real_connect(conn_ptr,"localhost","rick","rick","foo",0,NULL,0);

  18.  
  19. if(conn_ptr)

  20. printf("Connection success\n");

  21. else

  22. printf("Connection failed\n");

  23.  
  24. mysql_close(conn_ptr);

  25.  
  26. return EXIT_SUCCESS;

  27. }

Results of the:

 

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

If you do not install the development package at the beginning, the following error will be generated:

 

Execute SQL statements for data manipulation

Insert a row into the database table children:

 

 
  1. #include <stdlib.h>

  2. #include <stdio.h>

  3.  
  4. #include "mysql.h"

  5.  
  6. int main()

  7. {

  8. MYSQL my_connecyion;

  9. int res;

  10.  
  11. mysql_init(&my_connecyion);

  12.  
  13. if(mysql_real_connect(&my_connecyion,"localhost","rick","rick","foo",0,NULL,0))

  14. {

  15. printf("Connection success\n");

  16.  
  17. //执行SQL语句

  18. res = mysql_query(&my_connecyion,"INSERT INTO children(fname,age) VALUES('Ann',3)");

  19.  
  20. if(!res)

  21. printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connecyion));

  22. else

  23. fprintf(stderr,"Insert error %d : %s \n",mysql_errno(&my_connecyion),mysql_error(&my_connecyion));

  24.  
  25. mysql_close(&my_connecyion);

  26. }

  27. else{

  28. fprintf(stderr,"Connection failed\n");

  29. if(mysql_errno(&my_connecyion))

  30. fprintf(stderr,"Connection error %d : %s\n",mysql_errno(&my_connecyion),mysql_error(&my_connecyion));

  31. }

  32.  
  33. return EXIT_SUCCESS;

  34. }


operation result:

 

 

Special attention should be paid here:

The function mysql_affected_rows returns the number of rows modified by an update operation, not the number of rows satisfying the where clause.

Guess you like

Origin blog.csdn.net/xqhrs232/article/details/113182149