C++ 使用C++;用于MFC对话框基础应用程序

C++ 使用C++;用于MFC对话框基础应用程序,c++,mysql,visual-c++,mfc,C++,Mysql,Visual C++,Mfc,我有两个变量,我想把它们的值插入MySQL数据库,但我不知道怎么做 以下是迄今为止我的所有代码,请更正/建议: void RegistrationForm::Register() { istifadeciAdi.GetWindowText(i_ad); par.GetWindowText(i_par); parTekrar.GetWindowText(i_par_tekrar); if (istifadeciAdi.GetWindowTextLength() != 0)

我有两个变量,我想把它们的值插入MySQL数据库,但我不知道怎么做

以下是迄今为止我的所有代码,请更正/建议:

void RegistrationForm::Register()
{
    istifadeciAdi.GetWindowText(i_ad);
    par.GetWindowText(i_par);
    parTekrar.GetWindowText(i_par_tekrar);

if (istifadeciAdi.GetWindowTextLength() != 0) // if you can please write this line better.
{
    if (i_parol == i_parol_tekrar)
    {
        MySQL_Driver *driver;
        Connection *dbConn;
        Statement *statement;
        //ResultSet *result; // I don't need this line any more
        //PreparedStatement *ps;

        driver = get_mysql_driver_instance();
        dbConn = driver->connect("host", "u", "c");

        dbConn->setSchema("mfc_app_database");
        statement = dbConn->createStatement();

        statement->executeQuery("INSERT INTO users(`username`, `password`) VALUES (/* how to use i_ad and i_par as variable to set column value? */)"); // executes the user "input"

        /*ps = dbConn->prepareStatement("INSERT INTO users(`username`, `password`, `name`) VALUES (?)");
        ps->setString(1, "cccc");
        ps->setString(2, "ffff);*/

        //delete result;
        //delete[] result;
        /*delete ps;
        delete[] ps;*/
        delete statement;
        delete[] statement; // don't use this line in your program as me
        delete dbConn;
        delete[] dbConn; // don't use this line in your program as me
    }
    else
        MessageBox(L"Şifrə dəqiq təkrar olunmalıdır.", L"Xəbərdarlıq", MB_ICONWARNING);
}
else
    AfxMessageBox(L"Boş qoymaq olmaz.");
}
编辑 没有任何错误。但当我点击(注册)按钮时,它显示:
程序停止工作

单击
Debug
按钮后,我将进入我编写的插入查询的行


p、 It’很抱歉我的英语不好

使用
CString
进行查询。
例如:

CString strQuery;
strQuery.Format(_T("INSERT INTO users(`username`, `password`) VALUES ('%s', '%s')"),i_ad, i_par);
executeQuery
(或其他查询命令)中使用此查询字符串之前,必须将其转换为
std::string
。因为,
execute、executeQuery和executeUpdate命令只接受
std::string

CT2CA临时字符串(查询)

std::字符串查询(tempString)

并在execute命令中使用此字符串

statement->executeQuery(query);

使用
CString
进行查询。
例如:

CString strQuery;
strQuery.Format(_T("INSERT INTO users(`username`, `password`) VALUES ('%s', '%s')"),i_ad, i_par);
executeQuery
(或其他查询命令)中使用此查询字符串之前,必须将其转换为
std::string
。因为,
execute、executeQuery和executeUpdate命令只接受
std::string

CT2CA临时字符串(查询)

std::字符串查询(tempString)

并在execute命令中使用此字符串

statement->executeQuery(query);
对于该MySQL连接器来说,如果查询不返回结果集,则使用
statement::execute()
,如果查询只有一行结果集,则使用
statement::executeQuery()

因此,对于SQL
INSERT-INTO
,您的问题可能是应该使用
execute

对于该MySQL连接器,如果查询不返回结果集,则使用
statement::execute()
;如果查询结果集为单行,则使用
statement::executeQuery()


因此,对于SQL
INSERT-INTO
,您的问题可能是应该使用
execute

你必须告诉我们到底是什么不起作用。发布错误消息和您可能拥有的任何其他信息。@MichaelOryl请重新阅读问题。我刚刚编辑了它。你必须告诉我们到底是什么不起作用。发布错误消息和您可能拥有的任何其他信息。@MichaelOryl请重新阅读问题。我刚编辑过,我读过。我也接受了答案并编辑了它。谢谢我读过了。我也接受了答案并编辑了它。谢谢