当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Syntax error at *insertsql & *querysql stage

问题描述:

- (BOOL) saveData:(NSString*)registerNumber name:(NSString*)name

department:(NSString*)department year:(NSString*)year;

{

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &database) == SQLITE_OK)

{

//here syntax error comes..

NSString *insertSQL = [NSString stringWithFormat:@ "insert into studentsDetail (regno,name, department, year) values ("\"%d\",\"%@\", \"%@\", \"%@\")",[registerNumber integerValue],name, department, year];

const char *insert_stmt = [insertSQL UTF8String];

sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);

if (sqlite3_step(statement) == SQLITE_DONE)

{

return YES;

}else {

return NO;

}

sqlite3_reset(statement);

}

return NO;

}

- (NSArray*) findByRegisterNumber:(NSString*)registerNumber

{

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &database) == SQLITE_OK)

{

NSString *querySQL = [NSString stringWithFormat:"select name, department, year from studentsDetail where regno=\"%@\" ",registerNumber];

const char *query_stmt = [querySQL UTF8String];

NSMutableArray *resultArray = [[NSMutableArray alloc]init];

if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)

{

if (sqlite3_step(statement) == SQLITE_ROW)

{

NSString *name = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];

[resultArray addObject:name];

NSString *department = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];

[resultArray addObject:department];

NSString *year = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];

[resultArray addObject:year];

return resultArray;

}else{

NSLog(@"Not found");

return nil;

}

sqlite3_reset(statement);

}

}

return nil;

}

Syntax error at the stage of *insertsql & *querysql.

Here SQL table is created dynamically & inserted values goes on sql table. I have not more knowledge of SQL.

网友答案:

Use this string

 NSString *insertSQL = [NSString stringWithFormat:@ "insert into studentsDetail (regno,name, department, year) values (\"\"%d\",\"%@\", \"%@\", \"%@\")",[registerNumber integerValue],
                                        name, department, year];
网友答案:

Use this string.

NSString *insertSQL = [NSString stringWithFormat:@ "insert into studentsDetail (regno,name, department, year) values (\"%d\",\"%@\", \"%@\", \"%@\")",num,name, department, year];

I think you have added extra " before \"%d\" so i have remove it in my string try that if it help.

分享给朋友:
您可能感兴趣的文章:
随机阅读: