iphone - 在我的 iOS 应用程序中导入我的 sql 文件

标签 iphone ios file sqlite import

我在我的 Mac OSX 上安装了 SQLite Studio,我在那里创建了我的模式,在 MySQL 中我使用 DBFork Manager 一个案例工具来制作我的模式,我导出 sql 文件然后导入到 MySQL 服务器,所以我想做同样的事情我的 iOS 应用程序使用 SQLite。

我的意思是,我制作了我的 SQLite 模式,然后我需要从 *.sql 文件导入我的应用程序,我给出了我使用 SQL 字符串的代码示例......希望你能帮助我!

- (void)viewDidLoad {
NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = dirPaths[0];

// Build the path to the database file
_databasePath = [[NSString alloc]
                 initWithString: [docsDir stringByAppendingPathComponent:
                                  @"contacts.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt =
        "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";

        if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {
            _status.text = @"Failed to create table";
        }
        sqlite3_close(_contactDB);
    } else {
        _status.text = @"Failed to open/create database";
    }
}
[super viewDidLoad];

最佳答案

我假设您在 SQLite 中有一个数据库文件,并且想要安装它以便在您的 iPhone 上使用。如果你在你的程序中编译它,那么它将在文件包中。然而,那是只读的,所以如果你想改变你的数据库,你需要将它复制到 iphone 的文档目录(或另一个更合适的目录)。查看这个 stackoverflow 问题的答案:

SQLite on iPhone

关于iphone - 在我的 iOS 应用程序中导入我的 sql 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14379249/

相关文章:

ios - textView resignFirstResponder 没有触发 UIKeyboardWillHideNotification

java - 如何将POI中的单元格输出转换为java中的文本文件

java - 有没有一种有效的方法可以将纹理加载为 .PNG 图像以用于简单的 2D 游戏

iphone - 应用程序在模拟器上崩溃,但在 iPod 3.1.3 上运行良好。当应用程序在模拟器中运行时, super 类的 init 方法没有被命中(即使在调试时)

iphone - 如何标准化 iPhone 应用程序的外观

iphone - 如何使 Xcode 自动完成委托(delegate)方法?

java - 在反向java中打印行

iphone - 如何让 UIScrollView 记住执行 Segue 后的位置?

ios - 退出时保存当前状态,重新打开应用程序时自动重新加载

ios - NSUserDefaults standardUserDefaults 未清除的问题