简体   繁体   English

如何将nsmutable数组添加到sqlite数据库表中

[英]How to add nsmutable array into sqlite database table

如何在sqlite数据库表中添加nsmutablearray?有人可以帮我编码吗?

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);
    }

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);
            }
        }

Here is a list of tutorials for using sqlite with iphone. 这是在iphone上使用sqlite的教程列表。 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. 如果您要在iPhone OS上使用数据库,我建议您使用Core Data。

This tutorial should match your app quite well. 教程应该与您的应用程序非常匹配。 Please ask if any more questions. 请问是否还有其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM