3

i am on final project making GOLyrics apps for ios. my apps can search lyric and favorited it. but i have problems in my add favorite lyric, and i do not know why, im newbie in objective C especially in ios programming. help me please.

here's my add to favorite method:

  -(void)insertFav:(Favorite*)favssssdsd{

fileMgrs = [NSFileManager defaultManager];
sqlite3_stmt *stmt=nil;

NSString *sqlQuery=[NSString stringWithFormat:@"INSERT or REPLACE INTO fav (username,title,artist,image,song,lyric) VALUES ('%@','%@','%@','%@','%@','%@');",favssssdsd.username,favssssdsd.lirikTitle,favssssdsd.lirikArtist,favssssdsd.lirikImage,favssssdsd.lirikSong,favssssdsd.lirik];
NSLog(@"query:%@",sqlQuery);
const char *sql =[sqlQuery cStringUsingEncoding:NSUTF8StringEncoding];

//NSLog(@"row:%d",row);
//Open db
NSString *cruddatabase = [self.GetCacheDirectory stringByAppendingPathComponent:@"TB.sqlite"];
sqlite3_open([cruddatabase UTF8String], &db);
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
@try
{
    if(sqlite3_step(stmt)==SQLITE_DONE)
    {
        NSLog(@"Succes insert fav");
    }
    else  NSLog(@"failed insert fav");


}
@catch (NSException *exception) {
    NSLog(@"An exception occured: %@", [exception reason]);
}

sqlite3_reset(stmt);

sqlite3_finalize(stmt);
sqlite3_close(db);
   }

the output is :

TestYey[1505:47536] query:INSERT or REPLACE INTO fav      (username,title,artist,image,song,lyric) VALUES ('shasapo','Stay','Rihanna','18.jpg','Stay.mp3','the lyrics in here');

TestYey[1505:47536] failed insert fav

Someone help me please :") i dont know why it isn't inserting

6
  • first time making it, it success. but now it doesnt work. Commented Jul 1, 2015 at 13:00
  • check your sql query in Sqlite Manager. Commented Jul 1, 2015 at 13:01
  • Try after Reset your simulator. Commented Jul 1, 2015 at 13:03
  • i've check the query, it works perfectly in sqlite manager. and i've reset my simulator, still doesnt work Commented Jul 1, 2015 at 13:06
  • I think it is problem of your database path..check it properlly.add Break point and check where app stuck. Commented Jul 1, 2015 at 13:07

1 Answer 1

1
  1. You should check which code retuned from sqlite3_step(stmt) in else clause. That will help you to figure out what's wrong.

  2. Second improvement -

I suggest you to use sqlite3_bind_ functions:

SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
and check if sqlite3_prepare_v2 returns 'OK' result.

And rewrite your method as

-(void)insertFav:(Favorite*)favssssdsd{

    fileMgrs = [NSFileManager defaultManager];
    sqlite3_stmt *stmt=nil;


    char sql[] ="INSERT or REPLACE INTO fav (username,title,artist,image,song,lyric) VALUES (?,?,?,?,?,?);";

    NSString *cruddatabase = [self.GetCacheDirectory stringByAppendingPathComponent:@"TB.sqlite"];

    if (sqlite3_open([cruddatabase UTF8String], &db) != SQLITE_OK)
    {
       NSLog(@"Failed to open database");
        return;
    }

    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK)
    {

        int bindRes = 0;
        bindRes = sqlite3_bind_text(statement, 1, [favssssdsd.username UTF8String],-1, SQLITE_TRANSIENT);
        bindRes = sqlite3_bind_text(statement, 1, [favssssdsd.lirikTitle UTF8String],-1, SQLITE_TRANSIENT);

        bindRes = sqlite3_bind_text(statement, 1, [favssssdsd.lirikArtist UTF8String],-1, SQLITE_TRANSIENT);
        bindRes = sqlite3_bind_text(statement, 1, [favssssdsd.lirikImage UTF8String],-1, SQLITE_TRANSIENT);

        bindRes = sqlite3_bind_text(statement, 1, [favssssdsd.lirikSong UTF8String],-1, SQLITE_TRANSIENT);
        bindRes = sqlite3_bind_text(statement, 1, [favssssdsd.lirik UTF8String],-1, SQLITE_TRANSIENT);

        if(sqlite3_step(stmt)==SQLITE_DONE)
        {
            NSLog(@"Succes insert fav");
        }
        else  NSLog(@"failed insert fav");


    }


    sqlite3_reset(stmt);

    sqlite3_finalize(stmt);
    sqlite3_close(db);
}

Set up breakpoint on you bindRes and check where is the problem.

NOTE i don't know exactly which type have your properties, so you should rewrite bind functions, if needed. i use bind_text (for NSString binding)

Hope this help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.