IOS input error in sqlite
I am in a final project building GOL apps for ios. my apps can search for lyrics and use them. but i'm having trouble adding favorite lyrics and i dont know why, new to lens C, especially ios programming. help me please.
here's my add to favorites 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);
}
output:
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: ") I don't know why it doesn't insert
source to share
-
You should check what code migrated from sqlite3_step (stmt) to else. This will help you figure out what is wrong.
-
Second improvement -
I suggest you use the 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 a breakpoint on you bindRes and check where the problem is.
NOTE . I don't know exactly what type your properties are, so you need to rewrite the binding functions if necessary. I am using bind_text (for NSString binding)
Hope for this help
source to share