Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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
C++ 如何在Sql参数中使用字符串?_C++_Sql_Sqlite - Fatal编程技术网

C++ 如何在Sql参数中使用字符串?

C++ 如何在Sql参数中使用字符串?,c++,sql,sqlite,C++,Sql,Sqlite,应该如何在SQL参数中插入字符串 大概是这样的: string clas = "Computer Science"; sql = "SELECT * from STUDENTS where CLASS='clas'"; 有两种方法可以做到这一点: 这是首选且更安全的方法。你可以像这样使用预先准备好的语句 一个简单但不太安全的选项,它容易受到SQL注入的攻击 有两种方法可以做到这一点: 这是首选且更安全的方法。你可以像这样使用预先准备好的语句 一个简单但

应该如何在SQL参数中插入字符串

大概是这样的:

string clas = "Computer Science";

sql = "SELECT * from STUDENTS where CLASS='clas'";

有两种方法可以做到这一点:

这是首选且更安全的方法。你可以像这样使用预先准备好的语句 一个简单但不太安全的选项,它容易受到SQL注入的攻击
有两种方法可以做到这一点:

这是首选且更安全的方法。你可以像这样使用预先准备好的语句 一个简单但不太安全的选项,它容易受到SQL注入的攻击 简单回答:

您只需执行以下操作:

字符串clas=计算机科学; sql=从Class='+clas+'的学生中选择*; 回答得好:

但是,我们可以做得更好。如果需要多值替换怎么办?请参阅下面的代码,它可以替换多个字符串。此外,如果需要,还可以编写sql注入检查。最棒的是,你只需要调用prepare函数就可以了

使用说明:

使用?你需要放一根绳子的地方。如果需要替换多个字符串,请在调用prepare函数时将所有字符串放入orderas参数中。另外,请注意prepare函数调用preparesql,{param_1,param_2,param_3,…,param_n}

[注意:它适用于c++11及更高版本。它不适用于c++11之前的版本。因此,在编译它时,使用-std=c++11标志和g++]

[p.S.]:如果有什么不清楚的地方,请随时提问。

简单回答:

您只需执行以下操作:

字符串clas=计算机科学; sql=从Class='+clas+'的学生中选择*; 回答得好:

但是,我们可以做得更好。如果需要多值替换怎么办?请参阅下面的代码,它可以替换多个字符串。此外,如果需要,还可以编写sql注入检查。最棒的是,你只需要调用prepare函数就可以了

使用说明:

使用?你需要放一根绳子的地方。如果需要替换多个字符串,请在调用prepare函数时将所有字符串放入orderas参数中。另外,请注意prepare函数调用preparesql,{param_1,param_2,param_3,…,param_n}

[注意:它适用于c++11及更高版本。它不适用于c++11之前的版本。因此,在编译它时,使用-std=c++11标志和g++]


[p.S.]:如果有什么不清楚的地方,请随时询问。

小心SQL注入。你应该使用准备好的语句。小心SQL注入。你应该使用事先准备好的陈述。这就是为什么我说第二种选择更可取,更安全谢谢你的反馈,我改变了顺序谢谢你的回答。我不想去任何安全的地方。第二个答案给了我这个错误:错误激活E0413没有合适的从std::basic_string到char*的转换函数存在任何想法?这就是为什么我说第二个选项是首选的,更安全谢谢反馈,我更改了顺序谢谢回答。我不想去任何安全的地方。第二个答案给了我这个错误:error active E0413没有合适的从std::basic_字符串到char*的转换函数存在任何想法?
string clas = "Computer Science";
sql = "SELECT * FROM Students WHERE Class=?";

// Prepare the request right here

preparedStatement.setString(1, clas);

// Execute the request down here
string clas = "Computer Science";
sql = "SELECT * FROM Students WHERE Class='" + clas + "'";
#include <iostream>
#include <string>
#include <initializer_list>

using namespace std;


// write code for sql injection if you think
// it necessary for your program
// is_safe checks for sql injection
bool is_safe(string str) {
    // check if str is sql safe or not
    // for sql injection
    return true; // or false if not sql injection safe
}

void prepare(string &sql, initializer_list<string> list_buf) {
    int idx = 0;
    int list_size = (int)list_buf.size();
    
    int i = 0;
    for(string it: list_buf) {
        // check for sql injection
        // if you think it's necessary
        if(!is_safe(it)) {
            // throw error
            // cause, sql injection risk
        }

        if(i >= list_size) {
            // throw error
            // cause not enough params are given in list_buf
        }

        idx = sql.find("?", idx);
        if (idx == std::string::npos) {
            if(i < list_size - 1) {
                // throw error
                // cause not all params given in list_buf are used
            }
        }
        sql.replace(idx, 1, it);

        idx += 1; // cause "?" is 1 char
        i++;
    }
}

// now test it
int main() {
    string sql = "SELECT * from STUDENTS where CLASS=?";
    string clas = "clas";

    prepare(sql, {clas});

    cout << sql << endl;


    string sql2 = "select name from class where marks > ? or attendence > ?";
    string marks = "80";
    string attendence = "40";

    prepare(sql2, {marks, attendence});

    cout << sql2 << endl;

    return 0;
}