Find the answer to your Linux question:
Results 1 to 2 of 2
Hello! I'm coding a simple method in C++ to insert a tuple in a small database. The database has a table called 'user', with 3 fields (id - the primary ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! striker07's Avatar
    Join Date
    Jul 2010
    Location
    Santa Rita do Sapucaí, Brazil
    Posts
    47

    [SQLite with C++] sqlite3_step causing Segmentation Fault


    Hello! I'm coding a simple method in C++ to insert a tuple in a small
    database. The database has a table called 'user', with 3 fields (id -
    the primary key, name, and a image).
    I mapped these fields in a class - id is an int, name is a std::string
    and image is a std::vector of unsigned char. In this point, I'm ok. To
    insert this in the database, I write the following method:

    Code:
    bool DatabaseClass::insertAdminUser(User user) {
    
           int rc;
           sqlite3_stmt *statement;
    
           // mydb is defined in the private area of the class
           rc = sqlite3_open_v2("database.db", &mydb, SQLITE_OPEN_READWRITE, NULL);
           if (rc != SQLITE_OK) {
                   sqlite3_close(mydb);
                   return false;
           }
    
           rc = sqlite3_prepare_v2(mydb, "INSERT INTO users (name, isadmin,
    photo) VALUES (?, 1, ?)", -1, &statement, NULL);
           if (rc != SQLITE_OK) {
                   sqlite3_finalize(statement);
                   sqlite3_close(banco);
                   return false;
           }
    
           int i = 0;
           rc = sqlite3_bind_text(statement, ++i, user.getName().c_str(), -1,
    SQLITE_TRANSIENT);
           if (rc != SQLITE_OK) {
                   sqlite3_finalize(statement);
                   sqlite3_close(banco);
                   return false;
           }
    
           rc = sqlite3_bind_blob(statement, ++i, (void*) user.getPhoto()[0],
    user.getPhoto().size(), SQLITE_STATIC);
           if (rc != SQLITE_OK) {
                   sqlite3_finalize(statement);
                   sqlite3_close(banco);
                   return false;
           }
    
           rc = sqlite3_step(statement); // The app crashes here!
           if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
                   sqlite3_finalize(statement);
                   sqlite3_close(mydb);
                   return false;
           }
    
           sqlite3_finalize(statement);
           sqlite3_close(banco);
    
           return true;
    }
    Anyone has any idea about what to do?

  2. #2
    Just Joined! striker07's Avatar
    Join Date
    Jul 2010
    Location
    Santa Rita do Sapucaí, Brazil
    Posts
    47
    Solved:

    Code:
    rc = sqlite3_bind_blob(statement, ++i, (void*) &user.getPhoto()[0],
    user.getPhoto().size(), SQLITE_STATIC);
    Missing &...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •