-1
- (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;
         }

阶段的语法错误*insertsql & *querysql。这里 SQL 表是动态创建的,并且插入的值在 sql 表上。我没有更多的SQL知识。

4

2 回答 2

0

使用这个字符串

 NSString *insertSQL = [NSString stringWithFormat:@ "insert into studentsDetail (regno,name, department, year) values (\"\"%d\",\"%@\", \"%@\", \"%@\")",[registerNumber integerValue],
                                        name, department, year];
于 2013-06-04T12:29:28.430 回答
0

使用这个字符串。

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

"我认为您之前已经添加了额外内容,\"%d\"因此我已在字符串中将其删除,如果有帮助,请尝试。

于 2013-06-04T12:37:12.810 回答