C中的LOAD DATA不允许使用的命令

时间:2015-06-16 09:49:35

标签: mysql c load-data-infile

我在C中遇到LOAD DATA的问题。

当我在命令行中执行LOAD DATA命令时它工作正常,但是当我在C中尝试相同的事情时它会说:

  

此MySQL版本不允许使用used命令。

这是我的代码:

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

void finish_with_error(MYSQL *con)
{
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
    exit(1);
}

int main(int argc, char **argv)
{
    MYSQL *con = mysql_init(NULL);

    if (con == NULL)
    {
        fprintf(stderr, "%s\n", mysql_error(con));
        exit(1);
    }

    if (mysql_real_connect(con, "localhost", "", "", "testdb", 0, NULL, 0) == NULL)
    {
        finish_with_error(con);
    }else{
    printf("MySQL client version: %s\n", mysql_get_client_info());
    }



    if (mysql_query(con, "LOAD DATA LOCAL INFILE '/home/lien/Dropbox/StageTolsma/1eOpdracht/TxtBestanden/01_0021_200912100000.txt' INTO TABLE input FIELDS TERMINATED BY ';=' LINES TERMINATED BY '\n;'"))
    {
        finish_with_error(con);
    }else{
        printf("File was uploaded to the database\n");
    }

    mysql_close(con);
    exit(0);
}

1 个答案:

答案 0 :(得分:0)

现在它正在使用更改来明确设置MYSQL_OPT_LOCAL_INFILE选项。

有用的链接

  1. Security Issues with LOAD DATA LOCAL
  2. mysql_options()
  3. MYSQL_OPT_LOCAL_INFILE选项信息

    MYSQL_OPT_LOCAL_INFILE (argument type: optional pointer to unsigned int)
    
    If no pointer is given or if pointer points to an unsigned int that has a nonzero value, the LOAD LOCAL INFILE statement is enabled.
    

    所以通过给出一个空值(相当于没有给出指针)来解决问题:

    mysql_options(con, MYSQL_OPT_LOCAL_INFILE, 0);
    

    工作代码

    #include <my_global.h>
        #include <mysql.h>
        #include <stdio.h>
    
    void finish_with_error(MYSQL *con)
    {
        fprintf(stderr, "%s\n", mysql_error(con));
        mysql_close(con);
        exit(1);
    }
    
    int main(int argc, char **argv)
    {
        MYSQL *con = mysql_init(NULL);
        mysql_options(con, MYSQL_OPT_LOCAL_INFILE, 0);  
    
        if (con == NULL)
        {
            fprintf(stderr, "%s\n", mysql_error(con));
            exit(1);
        }
    
        if (mysql_real_connect(con, "localhost", "", "", "testdb", 0, NULL, 0) == NULL)
        {
            finish_with_error(con);
        }else{
        printf("MySQL client version: %s\n", mysql_get_client_info());
        }
    
    
    
        if (mysql_query(con, "LOAD DATA LOCAL INFILE '/home/lien/Dropbox/StageTolsma/1eOpdracht/TxtBestanden/01_0021_200912100000.txt' INTO TABLE input FIELDS TERMINATED BY ';=' LINES TERMINATED BY '\n;'"))
        {
            finish_with_error(con);
        }else{
            printf("File was uploaded to the database\n");
        }
    
        mysql_close(con);
        exit(0);
    }