Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Linux编程 - 使用C在MySQL中插入数据

Linux编程 - 使用C在MySQL中插入数据1. 代码编写#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"int main(int argc, char *argv[])
{
    MYSQL my_connection;
    int res;
    mysql_init(&my_connection);
   
    if (mysql_real_connect(&my_connection, "localhost", "root", "mysql", "mysql", 0, NULL, 0))
    {
        printf("Connection success ");
        res = mysql_query(&my_connection, "INSERT INTO children(fname, age) VALUES("david", 8)");
       
        if (!res)
        {
            printf("Inserted %lu rows ", (unsigned long)mysql_affected_rows(&my_connection));
        }
        else
        {
            fprintf(stderr, "Insert error %d: %s ", mysql_errno(&my_connection),
            mysql_error(&my_connection));
        }
       
        mysql_close(&my_connection);
    }
    else
    {
        fprintf(stderr, "Connection failed ");
        if (mysql_errno(&my_connection))
        {
            fprintf(stderr, "Connection error %d: %s ",
            mysql_errno(&my_connection), mysql_error(&my_connection));
        }
    }
       
    return EXIT_SUCCESS;
}2. 插入前3. 运行结果