ios - SQLite初始化的位置? (iOS)

标签 ios database sqlite

我正在为一个开源 iOS 应用程序做贡献,我需要在 SQLite 数据库首次初始化时向其插入行。但是由于我没有任何使用 SQL 的经验,我不知道应该在哪个文件中执行此操作,因此我一直在查看该项目数小时以查找数据传递的位置,但没有成功。这是我要添加行的表。我总共需要 4 行,名称为 avatar_clothes_01(已经存在)、avatar_clothes_02、avatar_clothes_03 和 avatar_clothes_04。提前致谢!

enter image description here

最佳答案

试试这个

Database queue definition

/**
 Database Queue object.
 */
var oDatabaseQueue : FMDatabaseQueue;

Init part

init() {

    let oDirPaths     = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true);
    let sDocsDir      = oDirPaths[0];
    let oDatabasePath = sDocsDir.stringByAppendingPathComponent("databaseName" + ".db");
    self.oDatabase    = FMDatabase(path: oDatabasePath as String);
    self.oDatabaseQueue = FMDatabaseQueue(path: oDatabasePath as String);
}

Inserting in your db, should be better if you pass your model as parameter and adjust the arguments according

func insertTest(id:Int,name:String,points:Int,purchased:Bool){
        self.oDatabaseQueue.inDatabase() { oDatabase in

            if (oDatabase?.open())! {

                var sSql = "INSERT INTO Clothes (";
                sSql = sSql + "ID,";
                sSql = sSql + "Name,";
                sSql = sSql + "Points,";
                sSql = sSql + "Purchased";
                sSql = sSql + ") VALUES (";
                sSql = sSql + "?,";
                sSql = sSql + "?,";
                sSql = sSql + "?,";
                sSql = sSql + "?";
                sSql = sSql + ")";
                Int(purchased)
                let oArguments : [Any] = [
                    id,
                    name,
                    points,
                    Int(purchased)
                ];

                if (database!.executeUpdate(sSql, withArgumentsIn: oArguments)) {

                    database!.commit()
                }
            }
        }
}

关于ios - SQLite初始化的位置? (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48475600/

相关文章:

ruby-on-rails - 一元多对多关系的 Rails 外键列名称约定

database - 如何在一个由多个应用程序组成的平台上实现持续交付,这些应用程序都依赖于一个数据库并且相互依赖?

android - 如何在单击按钮时在数据库中添加数据?

sqlite - 我想根据今天的日期而不是根据时间来获取数据(sequelize 和 sqlite)

ios - UIPageViewController 过渡协调器

ios - UITableView只显示部分部分,第三部分不显示

ios - Objective-C 运行时关联对象与 NSMutable 字典

ios - 在闭包中使用可变参数时编译器崩溃

javascript - React native SQLite 插入语句不起作用

iphone - sqlite3选择查询在IOS6中挂起