Linux C language links mysql examples
- 2020-05-30 21:57:27
- OfStack
Linux - C language links mysql examples
Step 1:
Install mysql, reference: https: / / www ofstack. com article / 39190. htm
Step 2:
Install the mysql.h function library
sudo apt-get install libmysqlclient-dev
After execution, you can see the /usr/include/MySQL directory
Then start our link.
First look at my database
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| chat_room |
| mysql |
| mysql_shiyan |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> use chat_room;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------+
| Tables_in_chat_room |
+---------------------+
| user_message |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from user_message;
+------+-------+--------+
| ID | name | passwd |
+------+-------+--------+
| 1 | linux | linux |
| 2 | lyt | lyt |
+------+-------+--------+
2 rows in set (0.00 sec)
As you can see, I have the user_message table in the chat_room database, and all we have to do is read out the data in this table.
Direct up code
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<mysql/mysql.h>
int main(void)
{
char *sql;
sql="SELECT * FROM user_message;";
int res;// perform sql The return flag after the statement
MYSQL_RES *res_ptr;// A pointer to the query result
MYSQL_FIELD *field;// Field structure pointer
MYSQL_ROW result_row;// Returns the query information by row
int row,column;// The number of rows and columns returned by the query
MYSQL *conn;//1 Two database link Pointers
int i,j;
// Initializes the connection handle
conn = mysql_init(NULL);
if(conn == NULL) { // If the return NULL Indicates that initialization failed
printf("mysql_init failed!\n");
return EXIT_FAILURE;
}
// Make the actual connection
// parameter conn Connection handle, host mysql Host or address ,user The user name ,passwd password ,database_name The database name , The rest is the default
conn = mysql_real_connect(conn,"localhost","lyt","","chat_room",0,NULL,0);
if (conn) {
printf("Connection success!\n");
} else {
printf("Connection failed!\n");
}
mysql_query(conn,"set names gbk");// Prevent garbled code. Setup and database encoding 1 There will be no confusion
res = mysql_query(conn,sql);// Right back 0
if(res) {
perror("my_query");
mysql_close(conn);
exit(0);
} else{
// Give me the query results res_ptr
res_ptr = mysql_store_result(conn);
// If the result is not empty , The output
if(res_ptr) {
column = mysql_num_fields(res_ptr);
row = mysql_num_rows(res_ptr);
printf(" To check the %d line \n",row);
// The field name of the output
for(i = 0;field = mysql_fetch_field(res_ptr);i++) {
printf("%10s",field->name);
}
puts("");
// Output the result by line
for(i = 1;i < row+1;i++){
result_row = mysql_fetch_row(res_ptr);
for(j = 0;j< column;j++) {
printf("%10s",result_row[j]);
}
puts("");
}
}
}
// Close the connection before exiting
mysql_close(conn);
return 0;
}
The results of
gcc -o mysql a.c -L/usr/lib/mysql -lmysqlclient
./mysql
Connection success!
To check the 2 line
ID name passwd
1 linux linux
2 lyt lyt
Notes written quite clearly, what is not clear can give me a message, we 1 piece of learning!