使用 C++ for MFC Dialog Base App 在 MySQL 中插入一行

Insert a row in MySQL using C++ for MFC Dialog Base App

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

这是我目前所有的代码,请 correct/advise:

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.");
}

编辑

没有任何错误。但是当我点击(注册)按钮时,它说:
Program stopped working
单击 Debug 按钮后,它会显示我编写的插入查询行。

p.s 抱歉我的英语不好。

使用CString进行查询。
例如:

CString strQuery;
strQuery.Format(_T("INSERT INTO users(`username`, `password`) VALUES ('%s', '%s')"),i_ad, i_par);

executeQuery(或其他查询命令)中使用此查询字符串之前,您必须将其转换为 std::string。因为,execute, executeQuery and executeUpdate 命令 只接受 std::string。所以,添加这行:

CT2CA tempString(query);

std::string query(tempString);

并在你的执行命令中使用这个字符串

statement->executeQuery(query);

那个 MySQL 连接器的 docs 说对没有 return 结果集的查询使用 statement::execute(),当有 statement::executeQuery() 时单行结果集。

所以对于 SQL INSERT INTO 也许你的问题是你应该使用 execute