问题描述
我有一个C ++ std :: string ,该文件已使用AES128加密,并希望将其写入sqllite数据库。我已经弄清楚了,我必须使用''和 <'个字符/ code>和 ,但似乎还有另一个问题。
I have a C++ std::string which is encrypted using AES128 and want to write it into a sqllite database. I figured out already, that I have to escape ' characters with '' and " with "", but there seems to be another problem.
它说:
unrecognized token: "'""\235\211g\264\376\247\3348( ]tu\202\346\360\226h\205D\322-\373\347y"
我的查询如下:
UPDATE tablename SET column='""\235\211g\264\376\247\3348( ]tu\202\346\360\226h\205D\322-\373\347y\315\|`\3206\245\220j6 \215&\301ww/\222R\352]\253,\362&\233ï\2530\322搜\377\321!\334t\224\271ќVu\214Z\\256""\242O\254\241\254\365\360<P\364\356\370\225jnۢ\231\335($\243\377fH\225\215\224\223\254\316' WHERE index='1';
使用未加密字符串的相同查询有效。任何想法吗?
The same query with the unencrypted string works. Any ideas?
推荐答案
您做错了。
您不应永远在查询中完整写出参数;但相反,您应该使用绑定参数:将值绑定到准备好的语句。
You should not, ever, write out the parameters in full within the query; but instead you should use bound parameters: Binding Values To Prepared Statements.
主要优势?绑定参数不必被转义,这可以完全防止SQL注入的任何风险,还可以大大简化您的工作!
The main advantage ? Bound parameters do not have to be escaped, which completely prevents any risk of SQL injections, and also greatly simplifies your life!
此外,准备好的语句也可以重复使用以提高效率,所以让我举一个完整的例子。
Also, prepared statements can be reused for greater efficiency, so let me give a full example.
// // WARNING: for concision purposes there is no error handling // and no attempt at making this code even remotely exception-safe. // // !!! DO NOT USE IN REAL LIFE !!! // void update(std::map<int, std::string> const& blobs) { // 1. Prepare statement sqlite3_stmt *stmt; sqlite3_prepare(db, "update tablename set column = ? where index = ?", -1, // statement is a C-string &stmt, 0 // Pointer to unused portion of stmt ); // 2. Use statement as many times as necessary for (auto const& pair: blobs) { int const index = pair.first; std::string const& blob = pair.second; // 2.1 Bind 1st parameter sqlite3_bind_text(stmt, 1, // 1-based index: 1st parameter blob.data(), blob.size(), 0 // no need for sqlite to free this argument ); // 2.2 Bind 2nd parameter sqlite3_bind_int(stmt, 2, // 1-based index: 2nd parameter index ); // 2.3 Execute statement sqlite3_step(stmt); // 2.4 Reset bindings sqlite3_reset(stmt); } // 3. Free prepared query sqlite3_finalize(stmt); } // update
注意:您当然可以保留准备好的语句甚至更长的时间。
这篇关于带有加密的std :: string的sqllite查询(无法识别的令牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!