objective-c - 整数未存储在数据库中(sqlite3)

标签 objective-c ios sqlite

我已根据我对上一篇文章中收到的不同答案的理解更改了代码。代码如下:

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);
}   
}

但现在的问题是什么都没有插入。日志显示“添加记录失败”。

我真的搞砸了这些数据库的东西,需要你的帮助。

最佳答案

您正在用引号将插入语句中的整数/浮点值括起来。不要那样做。

您的 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 ];
          }

关于objective-c - 整数未存储在数据库中(sqlite3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10931328/

相关文章:

android - 如何将列添加到 Activity 的 android 数据库表中?

android - 什么时候在非 Activity 类中调用 onCreate()?

Python SQLITE 插入不工作

iphone - Objective - C 从网络下载图片问题

iPhone:自动发布 - 何时发布(澄清)?

ios - 在 iPhone 上创建全景图的最佳方式?

ios - 将测试 iPhone 6S 更新到 11.1?

从 UIImages 创建 mov 时的 iOS 绿色边框

Objective-C 中的 HTML 字符串

ios - Cordova iOS 应用程序在启动 Storyboard和第一个屏幕之间获取白色闪光