QSqlQuery 行影响结果

QSqlQuery row affected result

SHA256 sha256;
std::string s = sha256(pass.toStdString());

QString myquery = "declare @identifier nvarchar(100) = NEWID()\
        declare @user_name nvarchar(50) = '"+user+"'\
        declare @pass_word nvarchar(100) = '"+QString::fromStdString(s)+"'\
        declare @hint nvarchar(50) = '"+hint+"'\
        if NOT exists(select * from user_table where (userid=@identifier or username = @user_name))\
        insert into user_table (username,password,password_salt,userid) values(@user_name,@pass_word,@hint,@identifier)";
qDebug()<<myquery;
openSqlConnection();
QSqlQuery q3;
q3.exec(myquery);

执行此查询后,如果执行成功,我应该会看到结果 (1 row(s) affected)

如果username已经存在于数据库中,结果将是

Command(s) completed successfully.

要查看 select 的结果,我使用 q3.next()

我如何知道我的查询已成功执行?

How do I know that my query has been executed successfully?

这里有多种选择。您可以检查:

Returns true and sets the query state to active if the query was successful; otherwise returns false.

例如:

if (!q3.exec(myquery))
    \ show error

An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.

例如:

q3.exec(myquery);

if (!q3.isActive())
    \ show error

QSqlError::NoError 0 No error occurred.

例如:

q3.exec(myquery);

if (q3.lastError().type() != QSqlError::NoError)
    \ show error

如果您通过了检查(您选择的检查),您可以进一步处理您的查询,例如查看 username 是否已经存在以及有多少行受到影响。