我根据我对上一篇文章中收到的不同答案的理解改变了代码。代码如下:
float lastClicked = 0.0;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *contentDirectory;
NSArray *directoryPath;
const char *dbpath = [databasePath UTF8String];
directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
contentDirectory = [directoryPath objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [contentDirectory stringByAppendingPathComponent: @"MCFRatingDatabase.db"]];
NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:databasePath] == NO){
if (sqlite3_open(dbpath, &connectDB) == SQLITE_OK)
{
char *errorMessage;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGERATING (CONTENTNAME STRING, RATING FLOAT)";
if (sqlite3_exec(connectDB, sql_stmt, NULL, NULL, &errorMessage) != SQLITE_OK)
{
NSLog(@"FAILED TO CREAT TABLE");
}
sqlite3_close(connectDB);
} else {
NSLog(@"FAILED TO OPEN/CREATE DATABASE");
}
}
[filemanager release];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &connectDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM IMAGERATING WHERE CONTENTNAME = '%@'", titleString];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(connectDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
display = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
rate = sqlite3_column_double(statement, 2);
NSLog(@"contents are name = %@ and rating = %f", display, rate);
}else{
}
sqlite3_finalize(statement);
}
sqlite3_close(connectDB);
}
}
-(IBAction)rateSubmitClick:(id)sender{
sqlite3_stmt *statement;
NSString *insertSQL;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &connectDB) == SQLITE_OK){
if(isHalfClicked){
rating = lastClicked + 0.5;
insertSQL = [NSString stringWithFormat: @"INSERT INTO IMAGERATING (CONTENTNAME, RATING) VALUES (\"%@\", %f)", titleString, rating];
}else{
rating = lastClicked;
insertSQL = [NSString stringWithFormat: @"INSERT INTO IMAGERATING (CONTENTNAME, RATING) VALUES (\"%@\", %f)", titleString, rating];
}
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(connectDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE){
NSLog(@"Added records are = %@ %f", titleString, rating);
isRated=YES;
}else{
NSLog(@"FAILED TO ADD RECORD");
}
sqlite3_finalize(statement);
sqlite3_close(connectDB);
}
}
但现在问题是什么都没有插入。日志显示“未能添加记录”。
我真的搞砸了这个数据库的东西,需要你的帮助。
答案 0 :(得分:1)
在插入声明中,您将lastClicked
与评分相关联。您应该将评级链接到它。看起来像一个错字。
此外,在创建表时创建一个integer类型的字段,如果还要存储其他值,则应该为float。
修改:
从日志语句中可以清楚地看到,第二次没有将记录添加到数据库中。字符串RECORD ADDED
未被记录 - 日志行必须来自其他地方。
答案 1 :(得分:1)
使用引号将整数/浮点值包装在insert语句中。不要那样做。
你的SQL贱人应该是这样的:
INSERT INTO USERRATING (CONTENTNAME, RATING) VALUES ("mycontentname", 4.5);
作为一个例子。
或换句话说,你的代码应该是:
if(isHalfClicked)
{
rating=lastClicked+0.5;
insertSQL = [NSString stringWithFormat: @"INSERT INTO USERRATING (CONTENTNAME, RATING) VALUES (\"%@\", %f)",titleString,rating];
}
else {
insertSQL = [NSString stringWithFormat: @"INSERT INTO USERRATING (CONTENTNAME, RATING) VALUES (\"%@\", %d)",titleString,lastClicked ];
}