1

Please let me know how to insert arrays into the Sqlite table. I am passing 2 arrays as the parameter. Here is the code:

-(void)saveData:(NSMutableArray *)firstArray second:(NSMutableArray *)secondArray {

[self openDB];

NSString *createSQL = @"CREATE TABLE IF NOT EXISTS Table1"
“(EID INTEGER PRIMARY KEY, Array1 TEXT, Array2 TEXT);";
char *errorMsg;
if (sqlite3_exec (database, [createSQL UTF8String],
                  NULL, NULL, &errorMsg) != SQLITE_OK)
{
    sqlite3_close(database);
    NSAssert(0, @"Error creating table: %s", errorMsg);
}

    char *update = "INSERT OR REPLACE INTO Table1 (ID, Array1, Array2)"
    "VALUES (?, ?, ?);";
    errorMsg=NULL;
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(database, update, -1, &stmt, nil)
        == SQLITE_OK) {

        Please let me know how to insert the data.

        if (sqlite3_step(stmt) != SQLITE_DONE)
            NSAssert(0, @"Error updating table: %s", errorMsg);
        sqlite3_finalize(stmt);

}

}

2
  • What difficulty are you facing? Why don't you save your arrays in XML files inside sandboxed folder? Commented Jan 30, 2014 at 20:20
  • I have my values in firstArray and secondArray as it passed as parameters. But I dont know how can I insert these values into the tables. Commented Jan 30, 2014 at 20:24

3 Answers 3

3

You have multiple problems with what you're doing, which seems to stem from an unfamiliarity with SQLite. First of all, sqlite doesn't understand arrays as primitive values, not to mention NSMutableArrays. Second, you're trying to pass the values in as parameters, but you're not setting the parameters anywhere. I suggest this book which should give you a better understanding of SQLite, or go through the online tutorials at sqlite.org

Sign up to request clarification or add additional context in comments.

Comments

0

to insert array you have to use one to many relationship so you need to create table contans for ex id and array1 and array2 as composite primary key

Comments

0

The JSON1 extension provides basic support for managing (JSON) arrays. It requires SQLite version 3.9.0 (2015-10-14). For further details, see:

https://www.sqlite.org/json1.html

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.