<bdo id='O909p'></bdo><ul id='O909p'></ul>

<legend id='O909p'><style id='O909p'><dir id='O909p'><q id='O909p'></q></dir></style></legend>
<i id='O909p'><tr id='O909p'><dt id='O909p'><q id='O909p'><span id='O909p'><b id='O909p'><form id='O909p'><ins id='O909p'></ins><ul id='O909p'></ul><sub id='O909p'></sub></form><legend id='O909p'></legend><bdo id='O909p'><pre id='O909p'><center id='O909p'></center></pre></bdo></b><th id='O909p'></th></span></q></dt></tr></i><div id='O909p'><tfoot id='O909p'></tfoot><dl id='O909p'><fieldset id='O909p'></fieldset></dl></div>
  • <tfoot id='O909p'></tfoot>

      <small id='O909p'></small><noframes id='O909p'>

      1. sqlite3 在数据库中插入和读取 BLOB 数据

        时间:2023-10-08

            <bdo id='kMeuW'></bdo><ul id='kMeuW'></ul>
            • <tfoot id='kMeuW'></tfoot>
                <tbody id='kMeuW'></tbody>
              <i id='kMeuW'><tr id='kMeuW'><dt id='kMeuW'><q id='kMeuW'><span id='kMeuW'><b id='kMeuW'><form id='kMeuW'><ins id='kMeuW'></ins><ul id='kMeuW'></ul><sub id='kMeuW'></sub></form><legend id='kMeuW'></legend><bdo id='kMeuW'><pre id='kMeuW'><center id='kMeuW'></center></pre></bdo></b><th id='kMeuW'></th></span></q></dt></tr></i><div id='kMeuW'><tfoot id='kMeuW'></tfoot><dl id='kMeuW'><fieldset id='kMeuW'></fieldset></dl></div>

                <small id='kMeuW'></small><noframes id='kMeuW'>

                <legend id='kMeuW'><style id='kMeuW'><dir id='kMeuW'><q id='kMeuW'></q></dir></style></legend>
                1. 本文介绍了sqlite3 在数据库中插入和读取 BLOB 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I need to read and write BLOB data to a database. Here is my structure table

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

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

                  How can I insert it into database, and then retrieve from it?

                  环境:iPhone SDK 4.1 SQLite3 数据库.

                  Environment: iPhone SDK 4.1 SQLite3 database.

                  此代码失败:

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

                  推荐答案

                  谢谢大家!!在您的帮助下,我解决了问题,并希望像我一样为未来的初学者分享结果.)

                  Thanx all!! With yours help I'v solved problem and want to share results for future beginners like I am.)

                  -(void) addToDB
                  {
                      NSLog(@"
                  Creating 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(@"
                  Reading 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);
                  }
                  

                  这篇关于sqlite3 在数据库中插入和读取 BLOB 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL Server - 将行加入逗号分隔列表 下一篇:如何使用存储的 MYSQL 程序的表输出

                  相关文章

                2. <i id='8OLv7'><tr id='8OLv7'><dt id='8OLv7'><q id='8OLv7'><span id='8OLv7'><b id='8OLv7'><form id='8OLv7'><ins id='8OLv7'></ins><ul id='8OLv7'></ul><sub id='8OLv7'></sub></form><legend id='8OLv7'></legend><bdo id='8OLv7'><pre id='8OLv7'><center id='8OLv7'></center></pre></bdo></b><th id='8OLv7'></th></span></q></dt></tr></i><div id='8OLv7'><tfoot id='8OLv7'></tfoot><dl id='8OLv7'><fieldset id='8OLv7'></fieldset></dl></div>

                  1. <small id='8OLv7'></small><noframes id='8OLv7'>

                  2. <legend id='8OLv7'><style id='8OLv7'><dir id='8OLv7'><q id='8OLv7'></q></dir></style></legend>
                    <tfoot id='8OLv7'></tfoot>
                        <bdo id='8OLv7'></bdo><ul id='8OLv7'></ul>