我在插入和检索代码时遇到问题
没有运行时错误,但插入数据不起作用,检索代码仅检索数据库中的最后一条记录。
使用NSString
或NSMutableString
会更好吗?
NSLog(@"test");
NSString *sql1 = [NSString stringWithFormat:@"INSERT INTO User Values (2,'%@','','F','123','','','','','A','B','123','123',2)",name.text];
char *err;
sqlite3_exec(database, [sql1 UTF8String], NULL, NULL, &err)!= SQLITE_OK ;
const char *sql = "select * from User";
sqlite3_stmt *searchStatement;
if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(searchStatement) == SQLITE_ROW)
{
char * try = (char*)sqlite3_column_text(searchStatement, 1);
if (try){
item = [NSMutableString stringWithUTF8String:try];
}
else{
item = @"";
}
答案 0 :(得分:1)
问题似乎出现在您的检索代码中。 sqlite3_column_type方法使用从零开始的索引,但看起来您从位置1开始。因此,每次单步执行时,您都不会提取所需的信息。我不确定你为什么要得到最后一项,但是尝试从0位开始,看看是否有帮助。