与MySQL的C或C ++连接 [英] c or C++ connection with mysql
本文介绍了与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 i need detailed code with comment line description as well.
推荐答案
尝试一下
http://www.cyberciti.biz/tips/linux-unix- connect-mysql-c-api-program.html [ ^ ]
或
http://www.kitebird.com/mysql-book/ch06-3ed.pdf [ ^ ]
Try This
http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html[^]
OR
http://www.kitebird.com/mysql-book/ch06-3ed.pdf[^]
您好,
这是为了响应您的请求而执行的插入您的特殊用户的请求:
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屋!
查看全文