使用C将客户端输入插入MySQL数据库 [英] Insert client input into MySQL database with C

查看:109
本文介绍了使用C将客户端输入插入MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编写一个C程序来获取MySQL服务器的客户端输入?我正在使用Fedora作为操作系统.

How do I write a C program which gets the client input for MySQL server? I'm using Fedora as my OS.

这是我的代码:

#include<my_global.h>
#include<mysql.h>

main()
{
    char name[25];
    MYSQL *conn;
    conn=mysql_init(NULL);
    mysql_real_connect(conn,"localhost","root","","testdb",0,NULL,0);
    printf("Enter your name:");
    scanf("%s",name);
    mysql_query(conn,"CREATE TABLE Users(name VARCHAR(25))");

    mysql_query(conn,"INSERT INTO Users VALUES(name)");
    //mysql_query(conn,"INSERT INTO Users VALUES('Farhan')");
    //mysql_query(conn,"INSERT INTO Users VALUES('Dileep')");
    //mysql_query(conn,"INSERT INTO Users VALUES('RAJIV')");

    mysql_close(conn);
}

我希望客户端输入而不是常量值存储在MySQL数据库中.

I want the client input to be stored in MySQL database and not the constant values.

推荐答案

尝试将名称冲刺到您的查询中,如下所示:

Try sprintf'ing the name into your query, something like this:

int len = query_len + name_len + 1;
char * insert_query = (char *) malloc(len);
snprintf(insert_query, len, "INSERT INTO Users VALUES('%s')", name);

但是,在检查缓冲区长度时,尤其是

However, you'll need to take some care when checking the buffer lengths and especially escaping the name string.

这篇关于使用C将客户端输入插入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆