您好,以下是代码,我试图将数据插入数据库但我无法插入表中。
-(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)
{
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;
}
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
而不是将这些值添加到NSString。尝试使用sqlite3_bind。它可能有用。
const char *sql = "INSERT INTO studentsDetail (regno,name,department,year) VALUES(?,?,?,?)";
if(sqlite3_prepare_v2(cruddb, sql, 1, &statement, NULL)) {
sqlite3_bind_int(stmt, 1, [registerNumber integerValue]);
sqlite3_bind_text(stmt, 2, [name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, [department UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, [year UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_finalize(statement);
}
答案 1 :(得分:0)
在 -
中尝试单引号 NSString *insertSQL=[NSString stringWithFormat:@"insert into studentsDetail (regno,name,department,year)values(\'%d\',\'%@\',\'%@\',\'%@\')",[registerNumber integerValue],name,department,year];
这在类似情况下对我有用。您可以使用sqlite3_Bind_Blob数据类型,因为它将按原样存储数据。