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

  • SEARCH
  • Home
  • 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 8592569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:46:55+00:00 2026-06-11T23:46:55+00:00

I want to insert some values into an SQLite table. The query is: insert

  • 0

I want to insert some values into an SQLite table.

The query is:

insert into TableName (field1,field2,field3) values (field1val, field2val, field3val)

Let’s say the table has 20 fields. I want to choose which field should be inserted depending on this value which is user input. The rest of fields should have no value.

My current solution is:

QString valuesToInsertFieldNames("(");
QString valuesToInsert("(");
if(field1val != -1)
{
    valuesToInsertFieldNames+= "field1";
    valuesToInsert += QString("%1 ").arg(field1val);
}
if(field2val != -1)
{
    valuesToInsertFieldNames+= "field2";
    valuesToInsert += QString("%1").arg(field2val);
}
...
valuesToInsertFieldNames+= ")";
valuesToInsert += ")";
query.exec(QString("insert into TableName " + valuesToInsertFieldNames + 
                   "values" + valuesToInsert)

Is there some better way of doing it? Maybe some QSql functionality?

  • 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-11T23:46:56+00:00Added an answer on June 11, 2026 at 11:46 pm

    I would hold the names and values in a map, and construct the query in a cycle:

    //You hold the field names and the corresponding values here.
    //The map's key is the field name, the map's value is the value. 
    QVariantMap pairs;
    //this will hold the values to compare with to decide whether to print the value
    //or not
    QVariantList compareWithMe;
    
    QString fieldNames;
    QString fieldValues;
    
    int count = 0;
    QVariantMap::iterator it = pairs.begin();
    for( it; it != pairs.end(); it++ )
    {
        if( it.value().toInt() == compareWithMe[count] )
        {
            count++;
            continue;
        }
        fieldNames.append( it.key() );
        fieldValues.append( it.value().toString() );
        //fieldValues.append( QString("%1").arg(it.value()) ); if you like it this way. 
        count++;
    }
    
    //Note that I've placed the opening and closing braces here, 
    //saving you two lines of code:
    //fieldNames.append(")");
    //fieldValues.append(")");
    query.exec(QString(
               "INSERT INTO TableName (" 
               + fieldNames 
               + ") VALUES (" 
               + fieldValues 
               + ")"
               ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to insert some data into a table (id PK autoincrement, val) with
So, I want to insert some data into a MySQL table where the table
I want to insert some simple data the query is: INSERT INTO categories_lang (category_id,language_id,name,description,alias)
I need to create an SQL query to insert some data into a table
I'm using a prepared statement to insert some values into my table. Before doing
I want to insert some values to the table, there is an order such
I want to insert some values from a table to another in a sql
I have two tables and want to insert some column values from ane table
After creating a table (by migration), I want to insert some entries directly. How
I want to do some quick inserts but avoid duplicates into a Table. For

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.