我有一个iPhone / iPad应用程序,其中包含一些存储在属性列表中的数据。我正在扩展应用程序以处理多次出现的相同数据,遗憾的是它太过于依赖于正确的列表而只是将整个批次转换为sqlite3或核心数据所以我需要将属性列表存储在blob字段中的sqlite3表中
我使用[NSPropertySerialisation dataFromPropertyList:format:errorDescription]对数据进行了序列化,并且可以在测试中成功地将其反序列化为NSDictionary。我可以存储数据,但是当我检索它时,我无法正确地将它恢复到[NSPropertySerialisation propertyListFromData]。
这是NSDictionary对象:
{
companydetails = {
address1 = "Company Address";
"company_name" = "New Survey Company";
};
}
转换为序列化数据:
NSData *data = [NSPropertyListSerialization dataFromPropertyList:[Survey data] format:NSPropertyListXMLFormat_v1_0 errorDescription:&errStr];
哪个收益率:
<3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c646963 743e0a09 3c6b6579 3e636f6d 70616e79 64657461 696c733c 2f6b6579 3e0a093c 64696374 3e0a0909 3c6b6579 3e616464 72657373 313c2f6b 65793e0a 09093c73 7472696e 673e436f 6d70616e 79204164 64726573 733c2f73 7472696e 673e0a09 093c6b65 793e636f 6d70616e 795f6e61 6d653c2f 6b65793e 0a09093c 73747269 6e673e4e 65772053 75727665 7920436f 6d70616e 793c2f73 7472696e 673e0a09 3c2f6469 63743e0a 3c2f6469 63743e0a 3c2f706c 6973743e 0a>
然后我可以用以下方式取消序列化:
NSDictionary *revData = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:&errStr];
让我回到:
{
companydetails = {
address1 = "Company Address";
"company_name" = "New Survey Company";
};
}
这是我的表格架构:
CREATE TABLE surveys ( id integer primary key autoincrement, company_name text, address text, surveydata blob );
这是我对数据库的插入语句:
NSString *insSQL = [NSString stringWithFormat:@"INSERT INTO surveys (company_name, address, surveydata) VALUES (\"%@\", \"%@\", \"%@\")", SURVEY_DEFAULT_COMPANY, SURVEY_DEFAULT_ADDRESS, data];
const char *sql = [insSQL UTF8String];
哪个收益率:
INSERT INTO surveys (company_name, address, surveydata) VALUES ("New Survey Company", "Company Address", "<3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c646963 743e0a09 3c6b6579 3e636f6d 70616e79 64657461 696c733c 2f6b6579 3e0a093c 64696374 3e0a0909 3c6b6579 3e616464 72657373 313c2f6b 65793e0a 09093c73 7472696e 673e436f 6d70616e 79204164 64726573 733c2f73 7472696e 673e0a09 093c6b65 793e636f 6d70616e 795f6e61 6d653c2f 6b65793e 0a09093c 73747269 6e673e4e 65772053 75727665 7920436f 6d70616e 793c2f73 7472696e 673e0a09 3c2f6469 63743e0a 3c2f6469 63743e0a 3c2f706c 6973743e 0a>")
当我从另一个函数中的表中检索数据时,我得到了正确的blob数据:
<3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c646963 743e0a09 3c6b6579 3e636f6d 70616e79 64657461 696c733c 2f6b6579 3e0a093c 64696374 3e0a0909 3c6b6579 3e616464 72657373 313c2f6b 65793e0a 09093c73 7472696e 673e436f 6d70616e 79204164 64726573 733c2f73 7472696e 673e0a09 093c6b65 793e636f 6d70616e 795f6e61 6d653c2f 6b65793e 0a09093c 73747269 6e673e4e 65772053 75727665 7920436f 6d70616e 793c2f73 7472696e 673e0a09 3c2f6469 63743e0a 3c2f6469 63743e0a 3c2f706c 6973743e 0a>
然而,当我把它转换回属性列表时,我遇到了问题:
所以,我使用以下方式选择我的数据:
SELECT surveydata FROM surveys WHERE id = 2
sqlite3_column_blob(stmt,0)中的原始数据是:
NSString *blobRaw = [NSString stringWithFormat:@"%s", sqlite3_column_blob(stmt, 0)];
NSLog(@"Raw BLOB = %@", blobRaw);
Raw BLOB = <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c646963 743e0a09 3c6b6579 3e636f6d 70616e79 64657461 696c733c 2f6b6579 3e0a093c 64696374 3e0a0909 3c6b6579 3e616464 72657373 313c2f6b 65793e0a 09093c73 7472696e 673e436f 6d70616e 79204164 64726573 733c2f73 7472696e 673e0a09 093c6b65 793e636f 6d70616e 795f6e61 6d653c2f 6b65793e 0a09093c 73747269 6e673e4e 65772053 75727665 7920436f 6d70616e 793c2f73 7472696e 673e0a09 3c2f6469 63743e0a 3c2f6469 63743e0a 3c2f706c 6973743e 0a>
(注意:我只是选择一个字段,因此它位于第0列)
[NSPropertyListSerialisation propertyListFromData]期望NSData作为输入,所以我试图将blob转换为NSData:
NSData *blobData = [NSData dataWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
但如果我输出NSData,我得到:
<3c336333 66373836 64203663 32303736 36352037 32373336 39366620 36653364 32323331 20326533 30323232 30203635 36653633 36662036 34363936 65363720 33643232 35353534 20343632 64333832 32203366 33653061 33632032 31343434 66343320 35343539 35303435 20323037 30366336 39203733 37343230 35302035 35343234 63343920 34333230 32323264 20326632 66343137 30203730 36633635 32662032 66343435 34343420 32303530 34633439 20353335 34323033 31203265 33303266 32662034 35346532 32323020 32323638 37343734 20373033 61326632 66203737 37373737 32652036 31373037 30366320 36353265 36333666 20366432 66343435 34203434 37333266 35302037 32366637 30363520 37323734 37393463 20363937 33373432 64203331 32653330 32652036 34373436 34323220 33653061 33633730 20366336 39373337 34203230 37363635 37322037 33363936 66366520 33643232 33313265 20333032 32336530 61203363 36343639 36332037 34336530 61303920 33633662 36353739 20336536 33366636 64203730 36313665 37392036 34363537 34363120 36393663 37333363 20326636 62363537 39203365 30613039 33632036 34363936 33373420 33653061 30393039 20336336 62363537 39203365 36313634 36342037 32363537 33373320 33313363 32663662 20363537 39336530 61203039 30393363 37332037 34373236 39366520 36373365 34333666 20366437 30363136 65203739 32303431 36342036 34373236 35373320 37333363 32663733 20373437 32363936 65203637 33653061 30392030 39336336 62363520 37393365 36333666 20366437 30363136 65203739 35663665 36312036 64363533 63326620 36623635 37393365 20306130 39303933 63203733 37343732 36392036 65363733 65346520 36353737 32303533 20373537 32373636 35203739 32303433 36662036 64373036 31366520 37393363 32663733 20373437 32363936 65203637 33653061 30392033 63326636 34363920 36333734 33653061 20336332 66363436 39203633 37343365 30612033 63326637 30366320 36393733 37343365 2030613e>
奇怪的是,如果我通过[NSPropertySerialization propertyListFromData]传递此NSData,我得到:
<3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c646963 743e0a09 3c6b6579 3e636f6d 70616e79 64657461 696c733c 2f6b6579 3e0a093c 64696374 3e0a0909 3c6b6579 3e616464 72657373 313c2f6b 65793e0a 09093c73 7472696e 673e436f 6d70616e 79204164 64726573 733c2f73 7472696e 673e0a09 093c6b65 793e636f 6d70616e 795f6e61 6d653c2f 6b65793e 0a09093c 73747269 6e673e4e 65772053 75727665 7920436f 6d70616e 793c2f73 7472696e 673e0a09 3c2f6469 63743e0a 3c2f6469 63743e0a 3c2f706c 6973743e 0a>
与序列化输入和blob数据相同。
任何人都可以帮我解决如何存储序列化属性列表并从sqlite表中恢复的问题 - BLOB似乎是存储数据的正确方法,但我无法重新构建它。
答案 0 :(得分:2)
通常,blob数据类型不适合仅插入引号内的SQL语句。所以,而不是你的:
NSString *insSQL = [NSString stringWithFormat:@"INSERT INTO surveys (company_name, address, surveydata) VALUES (\"%@\", \"%@\", \"%@\")", SURVEY_DEFAULT_COMPANY, SURVEY_DEFAULT_ADDRESS, data];
您可能需要考虑:
NSString *insSQL = @"INSERT INTO surveys (company_name, address, surveydata) VALUES (?, ?, ?)";
if (sqlite3_bind_text(database, 1, [SURVEY_DEFAULT_COMPANY UTF8String]) != SQLITE_OK)
NSLog("Bind error on #%d: '%s'", 1, sqlite3_errmsg(database));
if (sqlite3_bind_text(database, 2, [SURVEY_DEFAULT_ADDRESS UTF8String]) != SQLITE_OK)
NSLog("Bind error on #%d: '%s'", 2, sqlite3_errmsg(database));
if (sqlite3_bind_blob(database, 3, [data bytes], [data length], SQLITE_STATIC) != SQLITE_OK)
NSLog("Bind error on #%d: '%s'", 3, sqlite3_errmsg(database));
我没有测试过上面的代码,但希望你能得到这个想法。不要自己构建SQL语句,而是使用sqlite3_bind
functions。
这是一个更完整的示例,说明如何序列化NSDictionary
,存储在数据库中,然后检索和反序列化。以下是在SQLite数据库中序列化和保存数据的方法:
- (IBAction)saveButtonClicked:(id)sender
{
sqlite3 *database;
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *databaseName = [documentsDirectory stringByAppendingPathComponent:@"test.db"];
if (sqlite3_open([databaseName UTF8String], &database) != SQLITE_OK)
{
NSLog(@"open failed");
return;
}
NSString *sql = @"CREATE TABLE IF NOT EXISTS company (company_id INTEGER PRIMARY KEY AUTOINCREMENT, company_details BLOB);";
if (sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL) != SQLITE_OK)
{
NSLog(@"create failed %s", sqlite3_errmsg(database));
sqlite3_close(database);
return;
}
NSDictionary *dictionary = @{@"name" : @"IBM",
@"street" : @"1 New Orchard Road",
@"city" : @"Armonk",
@"state" : @"New York",
@"postal" : @"10504-1722"};
NSData *data= [NSPropertyListSerialization dataFromPropertyList:dictionary
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
sql = @"INSERT INTO company (company_details) VALUES (?);";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) != SQLITE_OK)
{
NSLog(@"prepare failed %s", sqlite3_errmsg(database));
sqlite3_close(database);
return;
}
if (sqlite3_bind_blob(statement, 1, [data bytes], [data length], SQLITE_STATIC) != SQLITE_OK)
{
NSLog(@"bind failed %s", sqlite3_errmsg(database));
sqlite3_finalize(statement);
sqlite3_close(database);
return;
}
if (sqlite3_step(statement) != SQLITE_DONE)
{
NSLog(@"step failed %s", sqlite3_errmsg(database));
}
else
{
NSLog(@"success");
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
加载和反序列化数据:
- (IBAction)loadButtonClicked:(id)sender
{
sqlite3 *database;
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *databaseName = [documentsDirectory stringByAppendingPathComponent:@"test.db"];
if (sqlite3_open([databaseName UTF8String], &database) != SQLITE_OK)
{
NSLog(@"open failed");
return;
}
sqlite3_stmt *statement;
NSString *sql = @"SELECT * FROM company;";
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) != SQLITE_OK)
{
NSLog(@"prepare failed %s", sqlite3_errmsg(database));
return;
}
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSInteger companyId = sqlite3_column_int(statement, 0);
const void *blob = sqlite3_column_blob(statement, 1);
NSInteger bytes = sqlite3_column_bytes(statement, 1);
NSData *data = [NSData dataWithBytes:blob length:bytes];
NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:data
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:nil];
NSLog(@"%@", @{@"company_id" : @(companyId),
@"company_details" : dictionary});
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
答案 1 :(得分:1)
您将包含blob的ASCII表示的字符串放入数据库中:
INSERT INTO surveys(...) VALUES(..., "<3c3f786d...")
但是,在SQLite中,blob literal必须格式化为带有x
前缀的单引号字符串,并且必须包含<>
之类的任何其他字符:
INSERT INTO surveys(...) VALUES(..., x'3c3f786d...')
无论如何,为了避免像这样的格式化问题,首选使用Rob的答案中的参数。