Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将用户添加到mysql的c程序_Mysql_C_Mysql Connector - Fatal编程技术网

将用户添加到mysql的c程序

将用户添加到mysql的c程序,mysql,c,mysql-connector,Mysql,C,Mysql Connector,我将通过C程序向MySQL添加一个用户。我打算用MySQL做最后一年的项目。如果我问错了,不要误会我。我对这个概念有一个疑问,那就是我们可以添加一个新的用户,并拥有一些权限作为root用户。在那之后,我们是否需要为root用户注销。如果我这样做,如何注销根 //header files inclusion int main(void) { //declared MYSQL,MYSQL_REs,MYSQL_ROW variables. // here declared user

我将通过C程序向MySQL添加一个用户。我打算用MySQL做最后一年的项目。如果我问错了,不要误会我。我对这个概念有一个疑问,那就是我们可以添加一个新的用户,并拥有一些权限作为root用户。在那之后,我们是否需要为root用户注销。如果我这样做,如何注销根

//header files inclusion

int main(void)
{
    //declared MYSQL,MYSQL_REs,MYSQL_ROW variables.

    // here declared username,pwd,localhost,databasename

    // here declared variable to query

    con = mysql_init(NULL);

    /* connection to database */
    if(!mysql_real_connect(con,server,username,password,database,0,NULL,0)) {
    {
        printf("error in connection\n");
        exit(1);
    }

    printf("\n Enter the username to create a user in DB\n");
    scanf("%s",name);

    printf("\n Enter password \n");
    scanf("%s",pwd);

    sprintf(sql_addUser,"create user '%s'@'localhost' identified by '%s';",name,pwd);
    mysql_query(con,sql_addUser);

    sprintf(sql_grantAcc,"grant INSERT on '%s'.* to '%s'@'localhost';",databse,name);
    mysql_query(con,sql_grantAcc);
所以这里我们创建了一个root模式的用户。然后我需要使用新创建的用户进入MySQL,以便我可能需要注销或关闭连接。请给我一个如何改变用户模式的解决方案

我假设要更改用户模式,我们需要关闭已建立的连接,对吗

mysql_free_result(result);
    mysql_close(con);

con = mysql_init(NULL);
    if(!mysql_real_connect(con,server,name,pwd,database,0,NULL,0)) {
    {
        printf("error in connection\n");
        exit(1);
    }

如果您希望使用mysql\u real\u connect(),我认为最好在尝试以不同用户身份重新打开之前关闭连接,因为的API描述没有描述它如何处理这种情况(可能会工作,但进程可能会在mysql服务器上保持打开状态,等待超时)

但是,您可以使用API的mysql\u change\u user()调用

语法:

my_bool mysql_change_user(MYSQL *mysql, const char *user, const char *password, const char *db)

在各自的api描述中有更多信息,它是从MySQL 3.23.3开始实现的。

Hi@Qsp感谢您的响应。我正在为我所做的努力而奋斗,但你给了我解决问题的办法来做进一步的工作。我认为mysql\u change\u用户最适合我,因为我不会改变数据库的未来。