const char * sql_stmt中的语法错误

时间:2013-06-04 11:33:29

标签: ios objective-c sql

-(BOOL)createDB{
        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: @"student.db"]];
        BOOL isSuccess = YES;
        NSFileManager *filemgr = [NSFileManager defaultManager];
        if ([filemgr fileExistsAtPath: databasePath ] == NO)
        {
            const char *dbpath = [databasePath UTF8String];
            if (sqlite3_open(dbpath, &database) == SQLITE_OK)
            {
                char *errMsg;
                const char *sql_stmt =
                "create table if not exists studentsDetail (regno integer
                primary key, name text, department text, year text)";
                if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
                    != SQLITE_OK)
                {
                    isSuccess = NO;
                    NSLog(@"Failed to create table");
                }
                sqlite3_close(database);
                return  isSuccess;
            }
            else {
                isSuccess = NO;
                NSLog(@"Failed to open/create database");
            }
        }
        return isSuccess;
}

此处出现语法错误

const char *sql_stmt = "create table if not exists studentsDetail (regno integer
                        primary key, name text, department text, year text)";

2 个答案:

答案 0 :(得分:0)

如果要将字符串常量拆分为多个源行,则必须将其括起来 引号中的每个部分

const char *sql_stmt =
"create table if not exists studentsDetail (regno integer "
"primary key, name text, department text, year text)";

而不是:

const char *sql_stmt =
"create table if not exists studentsDetail (regno integer
primary key, name text, department text, year text)";

答案 1 :(得分:-1)

请用此行替换您的代码

const char *sql_stmt = [@"create table if not exists studentsDetail (regno integer primary key, name text, department text, year text)" UTF8String];