【发布时间】:2013-01-17 12:34:24
【问题描述】:
我的 Mac OSX 有 SQLite Studio,我在那里创建了我的模式,在 MySQL 中我使用 DBFork Manager 一个案例工具来制作我的模式,我导出 sql 文件然后导入 MySQL 服务器,所以我想做同样的事情为我使用 SQLite 的 iOS 应用程序。
我的意思是,我创建了我的 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];
}
【问题讨论】:
-
还有什么问题?你不知道如何读取文件吗?
-
我知道,但我认为我的问题是那个 sql 文件的目录,例如我在我的项目中添加了 SQL 文件,然后我使用: NSArray *path = NSSearchPathForDirectoriesInDomains(???, NSUserDomainMask,是的);所以,当应用程序完成时,我的项目路径将可用,以及如何将 NSString 转换为 char,因为在 sqlite_exec(_db, sql_stmt
标签: iphone ios file sqlite import