我正在为ios制作GOLyrics应用程序的最终项目。我的应用程序可以搜索歌词并收藏它。但我在添加最喜欢的歌词中有问题,我不知道为什么,我在目标C中的新手特别是在ios编程中。请帮帮我。
这是我最喜欢的方法:
-(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);
}
输出是:
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
有人请帮助我:") 我不知道它为什么不插入
答案 0 :(得分:1)
您应该检查在else子句中从sqlite3_step(stmt)重新调整的代码。这将帮助你弄清楚出了什么问题。
第二次改进 -
我建议你使用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.
并将您的方法重写为
-(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);
}
在bindRes上设置断点并检查问题所在。
注意我不确切知道哪个类型具有您的属性,因此如果需要,您应该重写绑定函数。我使用bind_text(用于NSString绑定)
希望这个帮助