URL未插入SQLite3数据库中

时间:2010-09-18 05:54:50

标签: cocoa-touch sqlite ipad ios

我正在尝试在SQLite3数据库中插入url链接但它崩溃了 有没有人知道它发生了什么?

如果我静态尝试插入任何网址,它会动态但当我获取导航网页的网址时....它们不会插入.... 请建议我如何使它工作。我正在使用SDK 3.2

这是我的插入功能:

-(BOOL)insertImageToDB:(UIImage *)screenImage forURL:(NSString *)webURL
{
    BOOL retValue=NO;
    sqlite3 *database;
    sqlite3_stmt *insert_statement = nil;

    //NSString *localURL = webURL;

    NSString *localURL = @"https://www.google.com/";

    if (sqlite3_open([[self sqlitePath] UTF8String], &database)==SQLITE_OK)
    {
        const char *sql = "insert into Web_Image (Screen_Image, url) Values (?,?)";
        if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) == SQLITE_OK) {

            NSData *data = UIImageJPEGRepresentation(screenImage, 0);
            if(data != nil)
            {
                sqlite3_bind_blob(insert_statement, 1, [data bytes], [data length], SQLITE_TRANSIENT);
            }
            else 
            {
                sqlite3_bind_blob(insert_statement, 1, nil, -1 , SQLITE_TRANSIENT);
            }
            sqlite3_bind_text(insert_statement, 2, [localURL UTF8String], -1, SQLITE_TRANSIENT);
            //sqlite3_bind_text(insert_statement, 2, [webURL UTF8String], -1, SQLITE_TRANSIENT);
            if(SQLITE_DONE == sqlite3_step(insert_statement)){
                retValue=YES;
            }
        }
        sqlite3_reset(insert_statement);
        sqlite3_finalize(insert_statement);
        sqlite3_close(database);
    }
    return retValue;
}   

其中,screenImage:是当前网页的截图。        webURL:是当前网页的网址。

1 个答案:

答案 0 :(得分:1)

对于静态WebPageUrl

NSString *localURL = @"https://www.google.com/";
localURL =[localURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

对于Dynamic WebPageUrl

NSURL * url = Any web page url.....
NSString *localURL =[NSString stringWithFormat:@"%@",url];
localURL =[localURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

现在您可以将此格式化的本地URL作为字符串插入数据库。