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 9197919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:10:43+00:00 2026-06-17T22:10:43+00:00

I am writing a function to prepare a sql query and execute it against

  • 0

I am writing a function to prepare a sql query and execute it against a sqlite db. I am using a query to insert values in to a table, the query looks like this

   "insert into files values (?, ?, ?, ?, ?, 0)";

A row is inserted but all values for text fields are empty except the last field which is 0. The first 5 fields are of type TEXT

    // If I hard code a value for value.data() then the row is inserted correctly with my hardcoded data, the exact line is below
    status = sqlite3_bind_text(ppStmt, index, /* if I hardcode it works*/ value.data(), -1, SQLITE_STATIC);

The full function is shown below.The list is created on stack by the calller, I am not sure why it is not replacing question marks with my string args, hardcoded ones work though.. no error codes are returned

    //db is already open before I call this
void MediaCache::prepareAndExecuteQuery(string query, list<string> args)
{

    sqlite3_stmt *ppStmt = 0;
    const char **pzTail = 0;
    int status = 0;

    if( sqlite3_prepare_v2(db, query.data(), query.length(), &ppStmt, pzTail) != SQLITE_OK )
    {
        string error = sqlite3_errmsg(db);
        //throw an exception
    } 

    if(ppStmt)
    {

        list<string>::iterator current = args.begin();
        int index = 1;

        for(current = args.begin() ; current != args.end(); current++)
        {
            string value = *current;            
            status = sqlite3_bind_text(ppStmt, index, value.data(), -1, SQLITE_STATIC);

            if(status != SQLITE_OK)
            {
                //log error;
            }

            index++;
        }   

        status = sqlite3_step(ppStmt);
        status = sqlite3_finalize(ppStmt);
        //sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
    }
    else
    {
        //ppStmt is null
        //throw an exception
    }

}
  • 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-17T22:10:44+00:00Added an answer on June 17, 2026 at 10:10 pm

    I suspect the problem is here:

    string value = *current;            
    status = sqlite3_bind_text(ppStmt, index, value.data(), -1, SQLITE_STATIC);
    

    In this case value goes out of scope before the execution of the statement takes place. So the pointers given to sqlite3_bind_text are no longer valid. To “fix” that, you could use SQLITE_TRANSIENT, which would force the library to make its own copy of the data before returning.

    Also, if you are not using C++11, I don’t believe string::data() is guaranteed to be NULL terminated in which case the -1 parameter could be incorrect. It should maybe be value.length() instead.

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

Sidebar

Related Questions

I'm writing a function which will drop a table if it already exists. It
I am writing following code to fetch a row from table: $query = SELECT
I have a user a object that looks like this. $user = new User();
I am currently writing a isRegistered function. This function is written within a class
Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
I am writing function that involve other function from base R with a lot
I'm currently writing a function what would create a zip file, which will be
I'm writing a function, which determine the number of useful bits of a 16
I am writing a function in Haskell that deals with numbers beyond the length
I am writing a function short getBits(short data, int p, int n) I have

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.