如何将数组写入用于Objective-C的sqlite数据库中? [英] How to write the array into the sqlite database for Objective-C?

查看:32
本文介绍了如何将数组写入用于Objective-C的sqlite数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将数据库中的数据放入数组中,现在我希望它通过使用插入查询将其发送回数据库中.

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?

推荐答案

插入数据库与从数据库中读取的内容几乎完全相反.您没有说数组的结构是什么,但是这里有一些框架代码,您应该能够适应您的需求:

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

这篇关于如何将数组写入用于Objective-C的sqlite数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆