sqlite3 insert and read BLOB data in database

后端 未结 5 554
刺人心
刺人心 2020-12-03 09:05

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 I         


        
5条回答
  •  我在风中等你
    2020-12-03 09:43

    This code fails:

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

    You are calling the SQLite functions in the correct order. According to the SQLite docs:

    The safest and easiest to remember policy is to invoke these routines in one of the following ways:

    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()

    In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), or sqlite3_column_text16() first to force the result into the desired format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to find the size of the result. Do not mix calls to sqlite3_column_text() or sqlite3_column_blob() with calls to sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes().

    However, buff2 is an integer, not a data pointer. sqlite3_column_bytes tells you how many bytes are in the blob.

提交回复
热议问题