Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

SQL SERVER error syntax



  • Hello.
    I have problem about sql sever when I run program qt found error.

    [Microsoft][QDBC SQL Server Driver][SQL SERVER] Incorrect syntax near the keyword 'IF'.
    [Microsoft][QDBC SQL Server Driver][SQL SERVER] Incorrect syntax near the keyword 'personal'.
    QODBC3:Unable to execute statement.

    Here my code:
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    mDatabase = QSqlDatabase::addDatabase("QODBC");
    mDatabase.setDatabaseName("DRIVER={SQL SERVER};SERVER=IE-ME-0435;DATABASE=employee;"
    "UID=sa;PWD=9094;");
    if(!mDatabase.open())
    {
    QMessageBox::critical(this, "Error", mDatabase.lastError().text());
    return;
    }
    QSqlQuery qry;
    if(!qry.exec("CREATE TABLE IF NOT EXISTS personal"
    "(id INTERGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
    "number TEXT NULL, email TEXT NULL)"))
    {
    QMessageBox::critical(this, "Error", qry.lastError().text());
    return;
    }
    mModel = new QSqlQueryModel(this);
    mModel->setQuery("SELECT * FROM personal");
    ui->tableView->setModel(mModel);
    }
    void MainWindow::on_record_btn_clicked()
    {
    QSqlQuery qry;
    if(!qry.exec(QString("INSERT INTO personal (id, number, email) "
    "VALUES ('%1', %2, %3)")
    .arg(ui->id->text().toInt())
    .arg(ui->number->text())
    .arg(ui->email->text())))
    {
    QMessageBox::critical(this, "Error", qry.lastError().text());
    return;
    }
    mModel->setQuery("SELECT * FROM personal");
    }

    Please help me I do something wrong.

    Thank in Advance!



  • @hannao said in SQL SERVER error syntax:

    statement

    Which query statement is failing?



  • Hi, just guessing but shouldn't it be ".. (id INTEGER.." instead of "... (id INTERGER..."?


Log in to reply