2022-04-22 11:25:30 +08:00
|
|
|
/*
|
2022-04-22 14:28:53 +08:00
|
|
|
A demo for mysql client, from https://zetcode.com/db/mysqlc/
|
2022-04-22 11:25:30 +08:00
|
|
|
*/
|
|
|
|
#include <mysql/mysql.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
void finish_with_error(MYSQL *con)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
|
|
mysql_close(con);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
MYSQL *con = mysql_init(NULL);
|
|
|
|
|
|
|
|
if (con == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", mysql_error(con));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_real_connect(con, "localhost", "root", "",
|
|
|
|
"testdb", 0, NULL, 0) == NULL)
|
|
|
|
{
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
2022-04-22 14:28:53 +08:00
|
|
|
//Create table
|
2022-04-22 11:25:30 +08:00
|
|
|
if (mysql_query(con, "DROP TABLE IF EXISTS cars")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "CREATE TABLE cars(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), price INT)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
2022-04-22 14:28:53 +08:00
|
|
|
// insert datas
|
2022-04-22 11:25:30 +08:00
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(1,'Audi',52642)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(2,'Mercedes',57127)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(3,'Skoda',9000)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(4,'Volvo',29000)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(5,'Bentley',350000)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(6,'Citroen',21000)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(7,'Hummer',41400)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_query(con, "INSERT INTO cars VALUES(8,'Volkswagen',21600)")) {
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
2022-04-22 14:28:53 +08:00
|
|
|
//query datas
|
|
|
|
if (mysql_query(con, "SELECT * FROM cars"))
|
|
|
|
{
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result(con);
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
finish_with_error(con);
|
|
|
|
}
|
|
|
|
|
|
|
|
int num_fields = mysql_num_fields(result);
|
|
|
|
|
|
|
|
MYSQL_ROW row;
|
|
|
|
|
|
|
|
while ((row = mysql_fetch_row(result)))
|
|
|
|
{
|
|
|
|
for(int i = 0; i < num_fields; i++)
|
|
|
|
{
|
|
|
|
printf("%s ", row[i] ? row[i] : "NULL");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql_free_result(result);
|
2022-04-22 11:25:30 +08:00
|
|
|
mysql_close(con);
|
|
|
|
exit(0);
|
|
|
|
}
|