如何在我的iOS项目中添加和执行.sql文件? [英] How to add and execute .sql files in my iOS project?

查看:1133
本文介绍了如何在我的iOS项目中添加和执行.sql文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了许多关于在iOS中使用SQLite数据库的教程,但没有找到任何直接引用.sql文件的教程。任何人都可以告诉我如何将现有的SQL数据库链接到我的应用程序?

I've found many tutorials on using SQLite databases in iOS, but haven't found anything that directly references .sql files. Can anybody tell me how I can link an existing SQL database to my app?

编辑:这是一个MySQL转储。我们有一个基于浏览器的闪存卡程序,我们现在正在为Android和iOS设备复制。该文件已经有语句来创建表和填充数据,但我不知道如何使用应用程序中的数据。我可能想使用NSSearchPathForDirectoriesInDomains?

It's a MySQL dump. We had a browser-based flashcard program, which we're now replicating for Android and iOS devices. The file already has statements to create the tables and populate the data, but I don't know how to use that data within the app. Do I maybe want to use NSSearchPathForDirectoriesInDomains?

推荐答案

我执行脚本作为iOS项目的一部分。我这样做的原因是创建数据库模式的同一系列脚本也更新数据库模式(在发布后,运行新的增量脚本以向前拉模式)。这里详细解释:

I execute scripts as part of an iOS project. The reason I do it is the same series of scripts that creates the database schema also updates the database schema (after release, new delta scripts are run to pull the schema forward). It's explained in detail here:

SQLITE与Android(设置数据库的最佳方法)

我将脚本添加为资源,然后执行它。有一些EXxxx日志调用,但在其他方面通用。注意,tail-sqlite的使用在脚本循环时执行一个语句,尾部作为它停止的标记。这里是我用来执行的函数:

I add the script as a resource in the project and then execute it. There's some EXxxx logging calls but otherwise generic. Note the use of tail - sqlite executes a statement at a time of the script looping through with tail as a marker where it left off. Here's the function I use to execute:

- (BOOL)executeScript:(NSString *)contents error:(NSError **)error
{
    ENHeading(@"executeScript");

    sqlite3_stmt *stmt = NULL;
    const char *zTail;
    int rc;

    zTail = [contents UTF8String];

    while(zTail != NULL && 0 < strlen(zTail)) 
    {
        ENDebug("zTail: \"%s\"\n", zTail);

        NSString *tailStr = [NSString stringWithUTF8String:zTail];
        NSString *trimmed = [tailStr stringByTrimmingCharactersInSet:
                             [NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([trimmed length] == 0)
        {
            ENInfo("ignoring trailing whitespace");
            break;
        }

        // Temporarily hold this until the end of the loop in case we need to report an error
        const char *newzTail;

        rc = sqlite3_prepare_v2(_sqlite3, zTail, -1, &stmt, &newzTail);
        if(SQLITE_OK != rc) 
        {
            ENError(@"prepare err:%@", [self errorMessage]);
            if (error != NULL) {
                *error = [[[ENSqliteError alloc] initWithErrorCode:ENSqliteErrorInvalidSql 
                                                            reason:[self errorMessage]] autorelease];
            }

            return NO;
        }

        rc = sqlite3_step(stmt);
        ENDebug(@"rc=%d", rc);
        switch (rc)
        {
            case SQLITE_ROW:
                ENError(@"statement returns rows, script ignores");
                break;

            case SQLITE_OK:
            case SQLITE_DONE:
                break;

            default:
                ENError(@"error");
                ENError(@"prepare err:%@", [self errorMessage]);
                if (error != NULL) {
                    *error = [[[ENSqliteError alloc] initWithErrorCode:ENSqliteErrorReadingRows 
                                                                reason:[self errorMessage]] autorelease];
                }

                return NO;
        }

        // For next time around the loop
        zTail = newzTail;

        // Clear up since we're about to prepare another
        sqlite3_finalize(stmt);
    } 

    return YES;
}

这篇关于如何在我的iOS项目中添加和执行.sql文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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