3

I have taken the data from database into array now I want it to send it back into database by using the insert query.

How can I insert the array of the data into the database?

2 Answers 2

2

Inserting into the database is pretty much the reverse of reading from it. You don't say what the structure of your array is but here is some skeleton code you should be able to adapt to your needs:

// Assuming you have the sqlite database opened already
// sqlite3 *sqldb = ...;

// Also assuming you have an array of MyItems holding your data
// NSMutableArray *items = ...;

sqlite3_stmt *insert_statement;

// Prepare the insert statement
const char*sql = "INSERT INTO mytable (field1, field2, field3) VALUES(?,?,?)";
sqlite3_prepare_v2(sqldb, sql, -1, &insert_statement, NULL);

// Iterate over an array of dictionaries
for (MyItem *item in items) {

    // Bind variables - assumed to all be integers
    sqlite3_bind_int(insert_statement, 1, item.field1);
    sqlite3_bind_int(insert_statement, 2, item.field2);
    sqlite3_bind_int(insert_statement, 3, item.field3);

    // Execute the insert
    if (sqlite3_step(insert_statement) != SQLITE_DONE) {
        NSLog(@"Insert failed: %s", sqlite3_errmsg(sqldb));
    }

    // Reset the statement
    sqlite3_reset(insert_statement);
}

// release the statement
sqlite3_finalize(insert_statement);
Sign up to request clarification or add additional context in comments.

Comments

0

Iterate through the array's contents and build up the INSERT query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.