Linux C MySQL语句
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char server[] = "localhost";
char user[] = "root";
char password[] = "123456";
char database[] = "mysql";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("%s \n", row[0]);
}
mysql_free_result(res);
mysql_close(conn);
printf("finish! \n");
return 0;
}
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
MYSQL *conn;
char server[] = "localhost";
char user[] = "root";
char password[] = "123456";
char database[] = "mysql";
char sql_create[] = "CREATE TABLE if not exists testdb.cart(id int, name varchar(20), price float);";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
// if (mysql_query(conn, " create database testdb"))
if (mysql_query(conn, sql_create))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
mysql_close(conn);
printf("finish! \n");
return 0;
}
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
MYSQL_ROW row;
char server[] = "localhost";
char user[] = "root";
char password[] = "123456";
char database[] = "mysql";
char sql_insert[] = "INSERT INTO testdb.cart(id, name, price) VALUES(1,' apple', 12.6);";
char sql_select[] = "SELECT * FROM testdb.cart;";
MYSQL *conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
}
if (mysql_query(conn, sql_insert))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, sql_select))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
MYSQL_RES *res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("%s \n", row[0]);
}
mysql_close(conn);
printf("finish! \n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main(void)
{
MYSQL *conn_ptr;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
// MYSQL_FIELD *fd;
int res, i, j;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
return EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", " mysql", 0, NULL, 0);
if (conn_ptr) {
res = mysql_query(conn_ptr, "SELECT * FROM testdb.cart"); //查询语句
if (res) {
printf("SELECT error:%s\n",mysql_error(conn_ptr));
} else {
res_ptr = mysql_store_result(conn_ptr); //取出结果集
if(res_ptr) {
printf("%lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));
j = mysql_num_fields(res_ptr);
while((sqlrow = mysql_fetch_row(res_ptr))) { //依次取出记
录
for(i = 0; i < j; i++)
printf("%s\t", sqlrow[i]); //输出
printf("\n");
}
if (mysql_errno(conn_ptr)) {
// fprintf(stderr, "Retrive error:s\n", mysql_error(conn_ ptr));
}
}
mysql_free_result(res_ptr);
}
} else {
printf("Connection failed\n");
}
mysql_close(conn_ptr);
return EXIT_SUCCESS;
}