根据我对上一篇文章中收到的不同答案的理解,我更改了代码。代码如下:
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);
}
}
但现在的问题是没有任何东西被插入。日志显示“未能添加记录”。
我真的搞砸了这个数据库的东西,需要你的帮助。