与mysql进行C或c ++连接以进行插入操作 [英] C or c++ connection with mysql for insert operation

查看:115
本文介绍了与mysql进行C或c ++连接以进行插入操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个C/C ++程序,将用户添加到MySQL.应该只允许用户插入"到给定的数据库中.这是实际的问题,请发送所有详细信息以及说明.

Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database. this is the actual question please send everything in detail along with the description.

推荐答案

您是否为此 Googled 了?
例如,"MySQL C API编程教程" [
Did you Googled for it?
For instance, "MySQL C API programming tutorial"[^] has everything you need about.


用于MySQL语法.

要添加新用户,请使用
创建用户语法 [ GRANT语法 [ http://dev.mysql.com/downloads/connector/c/下载连接器 [ ^ ]
For the MySQL syntax.

To add a new user use CREATE USER Syntax[^]

To grant access to do anything other then be able to login.

Use GRANT Syntax[^]


For the MySQL connector for C, download the connector her http://dev.mysql.com/downloads/connector/c/[^]




这是为了响应您的请求而执行的插入您的特殊用户的请求:

Hi,

Here are requests to be executed to insert your special user in response to your request :

CREATE 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT INSERT ON database.* TO 'newuser'@'localhost';



这是管理连接和请求执行到数据库的代码,基本内容:



Here is the code to manage connection and request execution to database, basic stuff :

#include <mysql.h>
#include <stdio.h>

main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

char *server = "localhost";
char *user = "root";
char *password = "PASSWORD"; /* set me first */
char *database = "mysql";

conn = mysql_init(NULL);

/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

res = mysql_use_result(conn);

/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);

/* close connection */
mysql_free_result(res);
mysql_close(conn);
}




希望对您有所帮助.




Hope this Helps.


这篇关于与mysql进行C或c ++连接以进行插入操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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