0

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

我的意思是,我制作了我的 SQLite Schema,然后我需要从 *.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];

}

4

1 回答 1

2

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

iPhone 上的 SQLite

于 2013-01-17T19:02:43.883 回答