Dipanjan Dutta
Dipanjan Dutta

Reputation: 85

Integer not stored in database (sqlite3)

I have changed the code based on my understanding of the different answers I received on my previous post. The code is as follows:

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

But now the problem is nothing is getting inserted. Log shows "FAILED TO ADD RECORD".

I am really messed up with this database stuff and need your help.

Upvotes: 1

Views: 175

Answers (2)

Mundi
Mundi

Reputation: 80265

In your insert statement you are linking lastClicked to the rating. You should link the rating to it instead. Looks like a typo.

Also, you create a field of type integer when you create the table, it should be float if you are also storing other values.


Edit:

From your log statements it is clear that the second time you are not adding the record to the database. The string RECORD ADDED is not being logged - the log line must come from somewhere else.

Upvotes: 1

wadesworld
wadesworld

Reputation: 13743

You are wrapping your integer/float values in the insert statement with quotes. Don't do that.

Your SQL stement should look like:

INSERT INTO USERRATING (CONTENTNAME, RATING) VALUES ("mycontentname", 4.5);

as an example.

Or put another way, your code should be:

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

Upvotes: 1

Related Questions