Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7987775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:14:45+00:00 2026-06-04T12:14:45+00:00

Problem is that when I exec sql insert query (I’m using Qt Creator and

  • 0

Problem is that when I exec sql insert query (I’m using Qt Creator and sqlite), two new rows are added into database instead of one.

Here’s code block

void MainWindow::on_insertButton_clicked(){
 db.open();
 QString name = ui->nameLineEdit->text();
 QString queryString = "INSERT INTO highscores (name, score) VALUES '%1',%2";
 QSqlQuery query(queryString.arg(name).arg(score));
 query.exec();
}

This code should insert values name and score into database just once but after query exec two same rows are inserted.

Anyone has an idea what’s the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:14:46+00:00Added an answer on June 4, 2026 at 12:14 pm

    Isntead of

    QString queryString = "INSERT INTO highscores (name, score) VALUES '%1',%2";
    

    you should write

    QString queryString = "INSERT INTO highscores (name, score) VALUES ('%1',%2)";
    

    SQL’s INSERT command expects a list of datasets after VALUES separated by commas, each of them in parentheses with their column values.

    The following command inserts one tuple into the table table:

    INSERT INTO table (col1, col2) VALUES (val1, val2);
    
    | col1 | col2 |
    +------+------+
    | val1 | val2 |
    

    Because you didn’t put the two values in parentheses you asked SQLite to insert two datasets with one value each:

    INSERT INTO table (col1, col2) VALUES val1, val2;
    
    | col1 | col2 |
    +------+------+
    | val1 |      |
    | val2 |      |
    

    (The funny thing is that I tested this with both my SQLite and MySQL installations. They both threw an error when I omitted the parentheses at all. But this may depend on the version and some options you are using.)

    Please never use QString::arg to fill in arguments of a SQL query!

    (Or any other method of string building, like concatenation.) This can lead to a possible SQL injection.

    In order to build statements with variable arguments you should use SQL prepared statements as provided by QSqlQuery bound values:

    QString queryString = "INSERT INTO highscores (name, score) VALUES (?,?)";
    QSqlQuery query(queryString);
    query.addBindValue(name);
    query.addBindValue(score);
    query.exec();
    

    Note that I did not quote the string argument in the prepared statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the exec(query_string) command on sql server. The matter is that the
I am trying to insert data into MS SQL server 2008 using Ole DB.
I'm using sqlite3_exec() function in order to execute an SQL Insert command. The problem
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I have a problem with a recursive SQL function. The original problem is that
So my problem is this. When using the Data Context ExecuteQuery(string query, params object[]
I'm using Delphi 7 and Firebird 1.5. I have a query that I create
We're using a SQL Server CE database. I understand that there are limitations to
I insert new record to Project table and after that I restore DB and
Simple problem that I can't figure out... How can I print a '%' character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.