-5

我正在尝试使用 sqlite 管理器创建数据库。但是这些值没有存储在数据库中。如果我单击保存按钮,警报消息将显示在这样的“数据插入失败”上。我正在尝试纠正这些问题。在这个案例我访问了很多教程。但我无法纠正我的问题。昨天我完全被这个问题阻止了。请给我任何想法或建议如何保存数据。谢谢大家访问这个问题。TC

数据库.m

// 创建数据库

-(BOOL)createDB

{

NSString *docsDir;

NSArray *dirPaths;


// Get the document directory


dirPaths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);


docsDir=dirPaths[0];


// Build the path to the database file


databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"Feedback.db"]];


BOOL isSuccess=YES;


NSFileManager *fileManager=[NSFileManager defaultManager];


if([fileManager fileExistsAtPath:databasePath]==0)


{


const char *dbpath=[databasePath UTF8String];


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


{


char *errMsg;


const char *sql_stmt= "create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)";


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;

}

// save data in the Database

-(BOOL) saveData:(NSString *)Traineeid Trainername:(NSString *)Trainername Traineename:(NSString *)Traineename Rating:(NSString *)Rating;


{

const char *dbpath=[databasePath UTF8String];


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


{


NSString *insertSQL=[NSString stringWithFormat:@"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%@\", \"%@\", \"%@\")",[Traineeid integerValue],Trainername,Traineename,Rating];


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;

}


FeebBackForm.m

-(IBAction)saveData:(id)sender

{

BOOL success=NO;

NSString *alertString = @"Data Insertion failed";

if (Traineeid.text.length>0 &&Trainername.text.length>0 &&Traineename.text.length>0 &&Rating.text.length>0)

{

success=[[DBManager getSharedInstance]saveData:Traineeid.text Trainername:Trainername.text Traineename:Traineename.text Rating:Rating.text];

}

else

{

alertString = @"Enter all fields";

}

if (success == NO) 

{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:alertString message:nil
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];


[alert show];


}

}
4

1 回答 1

0

您在 sql 语句中的 int 参数周围加上字符串引号。

这可能不是您唯一的问题,但您应该绑定参数。看起来您在 sql insert 语句中为整数值加上引号,这些值在表中定义为整数。

"create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)"

"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%@\", \"%@\", \"%@\")"

注意整数周围的双引号。

此外,注销数据库的路径(即使在模拟器中运行)。转到 sqlite cmd 行并确保 db 存在并且空表在那里。这有助于故障排除。

最后,看看@ fmdb sqlite包装器 - 它有助于使用 sqlite,但如果这是您的偏好,它的代码也显示了使用 sqlite raw 的良好模式。

这是我的一个示例中的一个类似函数,它显示了如何绑定参数。您还应该完成准备工作:

- (void)updateContact: (Contact*)contact error:(NSError**)error
{
    if (![self ensureDatabaseOpen:error])
    {
        return;
    }

    NSLog(@">> ContactManager::updateContact");

    // prep statement
    sqlite3_stmt    *statement;
    NSString *querySQL = @"update contacts set name=?,address=?,phone=? where id=?";
    NSLog(@"query: %@", querySQL);
    const char *query_stmt = [querySQL UTF8String];

    // preparing a query compiles the query so it can be re-used.
    sqlite3_prepare_v2(_contactDb, query_stmt, -1, &statement, NULL);     
    sqlite3_bind_text(statement, 1, [[contact name] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(statement, 2, [[contact address] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(statement, 3, [[contact phone] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_int64(statement, 4, [[contact id] longLongValue]);

    NSLog(@"bind name: %@", [contact name]);
    NSLog(@"bind address: %@", [contact address]);
    NSLog(@"bind phone: %@", [contact phone]);
    NSLog(@"bind int64: %qi", [[contact id] longLongValue]);

    // process result
    if (sqlite3_step(statement) != SQLITE_DONE)
    {
        NSLog(@"error: %@", sqlite3_errmsg(_contactDb));
    }

    sqlite3_finalize(statement);
}
于 2013-07-11T12:51:47.187 回答