sqlite3在数据库中插入并读取BLOB数据

时间:2010-11-18 13:57:59

标签: select insert sqlite blob

我需要读取和写入BLOB数据到数据库。这是我的结构表

    #define CREATE_TABLE_USERS_SQL "CREATE TABLE IF NOT EXISTS %@ ( \
UserID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
Name VARCHAR(50), \
Image BLOB);"

如何将其插入数据库,然后从中检索?

环境:iPhone SDK 4.1 SQLite3数据库。

此代码失败:

        NSData * buf2 = [[NSData alloc] init];
        sqlite3_column_blob(statement, 5);
        buf2 = sqlite3_column_bytes(statement, 5);
        user.image = [UIImage imageWithData: buf2];

5 个答案:

答案 0 :(得分:10)

Thanx all !!随着你的帮助我解决了问题,并希望像我一样分享未来初学者的成绩。)

-(void) addToDB
{
    NSLog(@"\nCreating db");
    NSString *str = @"CREATE TABLE IF NOT EXISTS Images (image1 BLOB);";
    int res = SQLITE_ERROR;


    res = sqlite3_open([@"aa.sql" UTF8String], &database);
    res = sqlite3_exec(database, [str UTF8String], NULL, NULL, NULL);

    sqlite3_stmt *updStmt =nil; 

    const char *sql = "INSERT INTO Images (image1) VALUES (?);";
    res = sqlite3_prepare_v2(database, sql, -1, &updStmt, NULL);

    if(res!= SQLITE_OK)
    {
        NSLog(@"Error while creating update statement:%s", sqlite3_errmsg(database));
    }

    UIImage *img = [UIImage imageNamed: @"flower.png"];
    NSData *imageData = [NSData dataWithData: UIImagePNGRepresentation(img)];

    res = sqlite3_bind_blob(updStmt, 1, [imageData bytes], [imageData length] , SQLITE_TRANSIENT);

    if((res = sqlite3_step(updStmt)) != SQLITE_DONE)
    {
        NSLog(@"Error while updating: %@", sqlite3_errmsg(database));
        sqlite3_reset(updStmt);
    } 

    res = sqlite3_reset(updStmt);
    res = sqlite3_close(database);
}

-(void) readFromDB
{
    NSLog(@"\nReading from db");

    NSString *query = @"SELECT image1 from Images";
    int res = SQLITE_ERROR;
    int len = 0;

    res = sqlite3_open([@"aa.sql" UTF8String], &database);

    sqlite3_stmt *statement;
    res = sqlite3_prepare_v2 (database, [query UTF8String], -1, &statement, nil);

    if (res == SQLITE_OK)
    {
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            len = sqlite3_column_bytes(statement, 0);
            NSData *imgData = [[NSData alloc] initWithBytes: sqlite3_column_blob(statement, 0) length: len];           


            UIImage *img = [[UIImage alloc] initWithData:imgData];

            self.view1.image = img;

        }
    }
    sqlite3_finalize(statement);

    res = sqlite3_close(database);
}

答案 1 :(得分:1)

除了选择客户端或编程接口外,使用情况与任何其他字符串字段没有区别。


[更新]我还没有幸运为iPhone开发,但我相信它与任何其他SELECT或INSERT查询相同。确保对BLOB进行编码并用单个撇号(')将其括起来,就像字符串一样。

答案 2 :(得分:0)

为什么不使用飞肉的Gus Mueller fmdbhttp://flyingmeat.com/

https://github.com/ccgus/fmdb

如果您发现fmdb有用,请去购买Flying Meat的优秀应用之一。 VoodooPad或Acron。

答案 3 :(得分:0)

// -----插入图片

  - (void)setImage:(UIImage *)theImage
 {

  if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
  {    
    NSString *insertProgrammeSql = @"INSERT INTO imageTable (imageName) VALUES (?)";

sqlite3_stmt *statement;

   if (sqlite3_prepare_v2(myDatabase, [insertProgrammeSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {

    NSData *imageData = UIImagePNGRepresentation(theImage);

         sqlite3_bind_blob(statement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);

    sqlite3_step(statement);
}
  sqlite3_close(myDatabase);
//  return YES;    
}   


  }

// --------获取图片

   -(UIImage *)getImage

    {

    UIImage *image = nil;


if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{   

NSString *selectSql = @"SELECT image FROM imageTable";

sqlite3_stmt *statement;
if (sqlite3_prepare_v2(myDatabase, [selectSql UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
    int length = sqlite3_column_bytes(statement, 0);
    NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0 ) length:length];

    image = [UIImage imageWithData:imageData];



  sqlite3_finalize(statement);

  }
     sqlite3_close(myDatabase);
  }
 return image;
 }

答案 4 :(得分:0)

  

此代码失败:

   NSData * buf2 = [[NSData alloc] init];
   sqlite3_column_blob(statement, 5);
   buf2 = sqlite3_column_bytes(statement, 5);
   user.image = [UIImage imageWithData: buf2];

您正在以正确的顺序调用SQLite函数。根据SQLite文档:

  

最安全,最容易记住的策略是调用这些例程   通过以下方式之一:

     
      
  • sqlite3_column_text()后跟sqlite3_column_bytes()
  •   
  • sqlite3_column_blob()后跟sqlite3_column_bytes()
  •   
  • sqlite3_column_text16()后跟sqlite3_column_bytes16()
  •   
     

换句话说,你应该调用sqlite3_column_text(),   sqlite3_column_blob()或sqlite3_column_text16()首先强制   得到所需的格式,然后调用sqlite3_column_bytes()或   sqlite3_column_bytes16()查找结果的大小。不要混在一起   通过调用调用sqlite3_column_text()或sqlite3_column_blob()   sqlite3_column_bytes16(),不要混合调用   sqlite3_column_text16(),调用sqlite3_column_bytes()。

但是,buff2是一个整数,而不是数据指针。 sqlite3_column_bytes告诉您blob中有多少字节。