手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux编程 - 使用C在MySQL中插入数据

[日期:2012-06-22] 来源:Linux社区  作者:davidgu [字体:大 中 小]

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\n");
        res = mysql_query(&my_connection, "INSERT INTO children(fname, age) VALUES('david', 8)");
       
        if (!res)
        {
            printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(&my_connection));
        }
        else
        {
            fprintf(stderr, "Insert error %d: %s\n", mysql_errno(&my_connection),
            mysql_error(&my_connection));
        }
       
        mysql_close(&my_connection);
    }
    else
    {
        fprintf(stderr, "Connection failed\n");
        if (mysql_errno(&my_connection))
        {
            fprintf(stderr, "Connection error %d: %s\n",
            mysql_errno(&my_connection), mysql_error(&my_connection));
        }
    }
       
    return EXIT_SUCCESS;
}

2. 插入前

3. 运行结果

linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款