简体   繁体   English

在 mysql 查询中使用变量,在 C++ MFC 程序中

[英]Using a variable in a mysql query, in a C++ MFC program

After extensive trawling of the internet, I still haven't found any solution for this problem.在互联网上进行了广泛的搜索后,我仍然没有找到解决此问题的任何方法。

I'm writing a small C++ app that connects to an online database and outputs the data in a listbox.我正在编写一个连接到在线数据库并在列表框中输出数据的小型 C++ 应用程序。

I need to enable a search function using an edit box, but I can't get the query to work while using a variable.我需要使用编辑框启用搜索功能,但在使用变量时无法使查询工作。

My code is:我的代码是:

res = mysql_perform_query (conn, "select distinct artist from Artists");
//res = mysql_perform_query (conn, "select album from Artists where artist = ' ' ");



while((row = mysql_fetch_row(res)) != NULL){
    CString str;
    UpdateData();

    str = ("%s\n", row[0]);

    UpdateData(FALSE);
    m_list_control.AddString(str);

}

The first " res = " line is working fine, but I need the second one to work.第一个“ res = ”行工作正常,但我需要第二个才能工作。 I have a member variable m_search_edit set up for the edit box, but any way that I try to include it in the sql statement causes errors.我为编辑框设置了一个成员变量m_search_edit ,但是我尝试将它包含在 sql 语句中的任何方式都会导致错误。

eg.例如。

res = mysql_perform_query (conn, "select album from Artists where artist = '"+m_search_edit+" ' ");

causes this error:导致此错误:

error C2664: 'mysql_perform_query' : cannot convert parameter 2 from 'class CString' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"错误 C2664:'mysql_perform_query':无法将参数 2 从 'class CString' 转换为 'char *' 没有可用的用户定义转换运算符可以执行此转换,或者无法调用该运算符”

And when I convert m_search_edit to a char* it gives me a " Cannot add 2 pointers" error.当我将m_search_edit转换为char*它给了我一个“无法添加 2 个指针”的错误。

Is there any way around this?有没有办法解决?

The problem here is that you are probably building for Unicode, which means that CString consists of wide characters.这里的问题是您可能正在为 Unicode 构建,这意味着 CString 由宽字符组成。 You can't directly concatenate an ASCII string with a wide character string (and you can't concatenate string literals with the + operator either).您不能直接将 ASCII 字符串与宽字符串连接(也不能将字符串文字与 + 运算符连接)。

I think the clearest way to build the query string here is by using the CT2CA macro to convert the contents of the edit control from Unicode to ASCII and CStringA::Format to insert them in the string我认为在这里构建查询字符串的最清晰方法是使用CT2CA宏将编辑控件的内容从 Unicode 转换为 ASCII 并使用CStringA::Format将它们插入到字符串中

CStringA query;
query.Format("select album from Artists where artist = '%s'", CT2CA(m_search_edit));
res = mysql_perform_query(conn, query);

And as Thomas pointed out, you should be aware that this leaves the door open for SQL injection...正如托马斯指出的那样,您应该意识到这为 SQL 注入打开了大门......

EDIT: I'm not sure where this mysql_perform_query API comes from, but from the error message you posted it looks like it also requires a writable buffer ( char * instead of const char * ).编辑:我不确定这个mysql_perform_query API 来自哪里,但从你发布的错误消息来看,它看起来也需要一个可写缓冲区( char *而不是const char * )。 Since I can't find documentation for it, I don't know how big it expects that buffer to be, but to get a modifiable buffer out of a CString, look into the GetBuffer() and ReleaseBuffer() methods:由于我找不到它的文档,我不知道它期望缓冲区有多大,但要从CString 中获取可修改的缓冲区,请查看GetBuffer()ReleaseBuffer()方法:

CStringA query;
query.Format(...); // Replace ... with parameters from above
char * buffer = query.GetBuffer(MAX_STRING_LENGTH); // make length big enough for mysql
res = mysql_perform_query(conn, buffer)
query.ReleaseBuffer();

EDIT2 (in response to latest comment): EDIT2(回应最新评论):

Thank you for providing the definition of your mysql_perform_query function.感谢您提供mysql_perform_query函数的定义。 When asking questions in the future, keep in mind it's helpful to know when you've created helper functions like this one.将来提出问题时,请记住,知道何时创建了这样的辅助函数会很有帮助。

In this case, your mysql_perform_query function never modifies the query string -- the only thing it does is pass it to mysql_query , which takes a const char * , so there's no reason you shouldn't declare its parameter const too.在这种情况下,你的mysql_perform_query函数永远不会修改查询字符串——它唯一做的就是将它传递给mysql_query ,它接受一个const char * ,所以你没有理由也不应该声明它的参数const Once you do that, you'll find my first answer works (no need for GetBuffer/ReleaseBuffer):一旦你这样做,你会发现我的第一个答案有效(不需要 GetBuffer/ReleaseBuffer):

MYSQL_RES *mysql_perform_query(MYSQL *conn, const char * query)
{
   // Body as written in comment.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM