0

How to add nsmutablearray into the sqlite database table?Can any one help me to code?

1
  • What is the data in your array?? Provide some code snippet , if possible. Commented Sep 24, 2012 at 11:25

3 Answers 3

4

You can use :

 for (int i = 0; i < [mutArray count]; i++)
    {
       NSString *string = [mutArray objectAtIndex:i];
       // insert query
       insert into table_name ('string') values(column_name);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

anything else without for loop. directly insert array ?
3

Use looping Controls to sort the array and insert each values

Example:

for(int itemIndex = 0; itemIndex < [yourArray count];itemIndex++){

     NSString *myname = [yourArray objectAtIndex:itemIndex];

    //insert myname to database.

}

To retrieve, you can use the sample below code

        if(sqlite3_open([path UTF8String], &dataBase) == SQLITE_OK)
        {
            NSString *query = [NSString stringWithFormat:@"select name from syncTable where Crc = %d",crc];         const char *sql = [query UTF8String];
            sqlite3_stmt *statement;
            if (sqlite3_prepare(dataBase, sql, -1, &statement, NULL) == SQLITE_OK)
            {
                char *name;
                unsigned int value;
                while(sqlite3_step(statement) == SQLITE_ROW)
                {
                    name = (char *)sqlite3_column_text(statement, 0);   
                    [yourArray addObject:[NSString stringWithUTF8String:name]];

                }
                sqlite3_finalize(statement);
            }
        }

4 Comments

it showing error on NSString *myname = [yourArrayCount objectAtIndex:itemIndex]; yourArraycount..
while accessing from database can i read directly as a array? or what shud i do..
while reading the array having only one value at index 0,remaining all returns null value; @Ramshad
log the "query" in console, copy and paste and execute in SQLite.. see the result :) also you log the retrieving values in code..
1

Here is a list of tutorials for using sqlite with iphone. Its not a matter of simulator or device. It will work fine with simulator also.

Create your db structure properly and work based on this tutorials.

Or

I'd recommend you to use Core Data if you want you work with databases on iPhone OS.

This tutorial should match your app quite well. Please ask if any more questions.

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.