我有SQLite Studio fot我的Mac OSX,我在那里创建了我的Schemas,在MySQL中我使用DBFork Manager案例工具来制作我的Schema,导出sql文件然后导入到MySQL服务器,所以我想用我的iOS应用程序使用SQLite做同样的事情。我想让我的SQLite架构,然后我需要从* .sql文件导入到我的应用程序,我给我的代码示例使用SQL字符串...希望你能帮助我!在我的iOS应用程序中导入我的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,YES); 所以,这是我的项目的路径,将在应用完成时可用,以及如何将NSString转换为字符,因为在sqlite_exec(_db,sql_stmt < - 这里是一个char,...) ;我必须插入一个字符而不是NSString,并且我将文件的文本恢复为NSData或NSString ...并且在iOS 6.0中已弃用: NSString * textFile = [[NSString alloc] initWithContentsOfFile:path]; – rokimoki