我有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