开发者

how to run sql script file in sqlite3 through objective-C

Here is my question, when interacting with Sqlite 3 through terminal, you can execute SQL statements stored in a txt file by executing this command:

 .read filename

Is there a way to do such thing through Objective-C code? i.e. I've got a sqlite3 db file connected in the code, an开发者_开发知识库d i'd like to run a script file programmatically.


I know this is a really old question. I just thought I'd leave the code that i used just in case someone else would find it useful

-(BOOL)execFile:(NSString*)filepath
{
    NSError* error;
    NSString* content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
    if(error)
    {
        return NO;
    }
    char* execError;
    int x = sqlite3_exec(_database, [content UTF8String], nil, nil, &execError);
    if(execError)
    {
        NSLog(@"%s", execError);
    }
    return x == SQLITE_OK;
}


You could forward the content of the file line by line, or in whole to the function exec

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜