Ios5 将sqlite数据库与iCloud同步
我有我的sqlite数据库。现在我想与icloud同步。我读过一些博客,说不支持sqlite与icloud同步。要么选择核心数据,要么“做核心数据所做的” 现在我不可能去寻找核心数据了。就像第二个选项“做一些类似于CoreData同步sqlite DBs的事情:将“事务日志”发送到iCloud,并用这些日志构建每个本地sqlite文件。” 任何人都可以分享sqlite“事务日志”的任何示例代码,或者可以逐步详细说明我需要做什么?1)我按照此链接创建了xml。您可以从其github下载相应的api。链接为- 2) 点击按钮:Ios5 将sqlite数据库与iCloud同步,ios5,Ios5,我有我的sqlite数据库。现在我想与icloud同步。我读过一些博客,说不支持sqlite与icloud同步。要么选择核心数据,要么“做核心数据所做的” 现在我不可能去寻找核心数据了。就像第二个选项“做一些类似于CoreData同步sqlite DBs的事情:将“事务日志”发送到iCloud,并用这些日志构建每个本地sqlite文件。” 任何人都可以分享sqlite“事务日志”的任何示例代码,或者可以逐步详细说明我需要做什么?1)我按照此链接创建了xml。您可以从其github下载相应的api
-(IBAction) btniCloudPressed:(id)sender
{
// create the document with it’s root element
APDocument *doc = [[APDocument alloc] initWithRootElement:[APElement elementWithName:@"Properties"]];
APElement *rootElement = [doc rootElement]; // retrieves same element we created the line above
NSMutableArray *addrList = [[NSMutableArray alloc] init];
NSString *select_query;
const char *select_stmt;
sqlite3_stmt *compiled_stmt;
if (sqlite3_open([[app getDBPath] UTF8String], &dbconn) == SQLITE_OK)
{
select_query = [NSString stringWithFormat:@"SELECT * FROM Properties"];
select_stmt = [select_query UTF8String];
if(sqlite3_prepare_v2(dbconn, select_stmt, -1, &compiled_stmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiled_stmt) == SQLITE_ROW)
{
NSString *addr = [NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,0)]];
addr = [NSString stringWithFormat:@"%@#%@",addr,[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,1)]];
addr = [NSString stringWithFormat:@"%@#%@",addr,[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,2)]];
addr = [NSString stringWithFormat:@"%@#%@",addr,[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,3)]];
addr = [NSString stringWithFormat:@"%@#%@",addr,[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,4)]];
addr = [NSString stringWithFormat:@"%@#%@",addr,[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,5)]];
addr = [NSString stringWithFormat:@"%@#%@",addr,[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,6)]];
addr = [NSString stringWithFormat:@"%@#%@",addr,[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,7)]];
//NSLog(@"%@",addr);
[addrList addObject:addr];
}
sqlite3_finalize(compiled_stmt);
}
else
{
NSLog(@"Error while creating detail view statement. '%s'", sqlite3_errmsg(dbconn));
}
}
for(int i =0 ; i < [addrList count]; i++)
{
NSArray *addr = [[NSArray alloc] initWithArray:[[addrList objectAtIndex:i] componentsSeparatedByString:@"#"]];
APElement *property = [APElement elementWithName:@"Property"];
[property addAttributeNamed:@"id" withValue:[addr objectAtIndex:0]];
[property addAttributeNamed:@"street" withValue:[addr objectAtIndex:1]];
[property addAttributeNamed:@"city" withValue:[addr objectAtIndex:2]];
[property addAttributeNamed:@"state" withValue:[addr objectAtIndex:3]];
[property addAttributeNamed:@"zip" withValue:[addr objectAtIndex:4]];
[property addAttributeNamed:@"status" withValue:[addr objectAtIndex:5]];
[property addAttributeNamed:@"lastupdated" withValue:[addr objectAtIndex:6]];
[property addAttributeNamed:@"deleted" withValue:[addr objectAtIndex:7]];
[rootElement addChild:property];
[property release];
APElement *fullscore = [APElement elementWithName:@"FullScoreReport"];
[property addChild:fullscore];
[fullscore release];
select_query = [NSString stringWithFormat:@"SELECT AnsNo,Answer,AnswerScore,MaxScore,AnsIndex FROM FullScoreReport WHERE Addr_ID = %@",[addr objectAtIndex:0]];
select_stmt = [select_query UTF8String];
if(sqlite3_prepare_v2(dbconn, select_stmt, -1, &compiled_stmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiled_stmt) == SQLITE_ROW)
{
APElement *answer = [APElement elementWithName:@"Answer"];
[answer addAttributeNamed:@"AnsNo" withValue:[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,0)]]];
[answer addAttributeNamed:@"Answer" withValue:[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,1)]]];
[answer addAttributeNamed:@"AnswerScore" withValue:[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,2)]]];
[answer addAttributeNamed:@"MaxScore" withValue:[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,3)]]];
[answer addAttributeNamed:@"AnsIndex" withValue:[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiled_stmt,4)]]];
[fullscore addChild:answer];
[answer release];
}
sqlite3_finalize(compiled_stmt);
}
}
sqlite3_close(dbconn);
NSString *prettyXML = [doc prettyXML];
NSLog(@"\n\n%@",prettyXML);
//***** PARSE XML FILE *****
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Properties.xml" ];
NSData *file = [NSData dataWithBytes:[prettyXML UTF8String] length:strlen([prettyXML UTF8String])];
[file writeToFile:path atomically:YES];
NSString *fileName = [NSString stringWithFormat:@"Properties.xml"];
NSURL *ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:fileName];
MyDocument *mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage];
mydoc.xmlContent = prettyXML;
[mydoc saveToURL:[mydoc fileURL]forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success)
{
if (success)
{
NSLog(@"XML: Synced with icloud");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iCloud Syncing" message:@"Successfully synced with iCloud." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
NSLog(@"XML: Syncing FAILED with icloud");
}];
}
5) 现在在你的办公室
- (void)loadData:(NSMetadataQuery *)queryData
{
for (NSMetadataItem *item in [queryData results])
{
NSString *filename = [item valueForAttribute:NSMetadataItemDisplayNameKey];
NSNumber *filesize = [item valueForAttribute:NSMetadataItemFSSizeKey];
NSDate *updated = [item valueForAttribute:NSMetadataItemFSContentChangeDateKey];
NSLog(@"%@ (%@ bytes, updated %@) ", filename, filesize, updated);
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
MyDocument *doc = [[MyDocument alloc] initWithFileURL:url];
if([filename isEqualToString:@"Properties"])
{
[doc openWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"XML: Success to open from iCloud");
NSData *file = [NSData dataWithContentsOfURL:url];
//NSString *xmlFile = [[NSString alloc] initWithData:file encoding:NSASCIIStringEncoding];
//NSLog(@"%@",xmlFile);
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:file];
[parser setDelegate:self];
[parser parse];
//We hold here until the parser finishes execution
[parser release];
}
else
{
NSLog(@"XML: failed to open from iCloud");
}
}];
}
}
}
6) 现在,在解析器方法中,获取数据并根据需要在数据库中插入/更新
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)nameSpaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:@"Property"])
{
NSLog(@"Property attributes : %@|%@|%@|%@|%@|%@|%@|%@", [attributeDict objectForKey:@"id"],[attributeDict objectForKey:@"street"], [attributeDict objectForKey:@"city"], [attributeDict objectForKey:@"state"],[attributeDict objectForKey:@"zip"],[attributeDict objectForKey:@"status"],[attributeDict objectForKey:@"lastupdated"],[attributeDict objectForKey:@"deleted"]);
}
//like this way fetch all data and insert in db
}
谢谢你的回答。我只是想问一下我能不能也在iCloud上发送图片。是的,我也发送了图片(比如图片文件夹)。我已经在这个链接上回答了同样的问题,我集成了上面的代码。现在它运行良好,但我想问一下,如果我在我的条目中更改了一些内容,它是如何管理的。请您详细说明您的声明好吗?基本上,数据库中的条目是由您自己管理的。现在,您只需创建现有已更改数据库的xml并将其发送到云。@Bhargavi这里您发送sqlite,然后通过将其转换为zipfile sp再次发送文件夹您为什么不能通过获取路径在项目的文件夹中获取sqlite,以同样的方式发送sqlite?您能告诉我我们必须创建xml吗每一次?或者仅使用该XML中的更改进行更新?如果只是改变,请告诉我怎么做?或者,如果创建XML,那么我认为这将是一个耗时的操作。。请建议..如果您有足够的灵活性来修改现有的XML,那就太好了。当然,您可以修改现有的一个:不需要每次都创建u plz可以让我知道如何对现有的进行更改吗?我没有对现有的进行任何更改。但我想你需要为此努力。在我的例子中,我每次都在创建XML,因为需要进行批量更改。如果您有密钥对值类型数据,那么您也可以选择plist文件。@ḯ 我在你的一个问题中读到,你想在你的一个应用程序中使用信用卡。我同样需要你的帮助。你完成那项任务了吗?是的,苹果批准了吗?
- (void)loadData:(NSMetadataQuery *)queryData
{
for (NSMetadataItem *item in [queryData results])
{
NSString *filename = [item valueForAttribute:NSMetadataItemDisplayNameKey];
NSNumber *filesize = [item valueForAttribute:NSMetadataItemFSSizeKey];
NSDate *updated = [item valueForAttribute:NSMetadataItemFSContentChangeDateKey];
NSLog(@"%@ (%@ bytes, updated %@) ", filename, filesize, updated);
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
MyDocument *doc = [[MyDocument alloc] initWithFileURL:url];
if([filename isEqualToString:@"Properties"])
{
[doc openWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"XML: Success to open from iCloud");
NSData *file = [NSData dataWithContentsOfURL:url];
//NSString *xmlFile = [[NSString alloc] initWithData:file encoding:NSASCIIStringEncoding];
//NSLog(@"%@",xmlFile);
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:file];
[parser setDelegate:self];
[parser parse];
//We hold here until the parser finishes execution
[parser release];
}
else
{
NSLog(@"XML: failed to open from iCloud");
}
}];
}
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)nameSpaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:@"Property"])
{
NSLog(@"Property attributes : %@|%@|%@|%@|%@|%@|%@|%@", [attributeDict objectForKey:@"id"],[attributeDict objectForKey:@"street"], [attributeDict objectForKey:@"city"], [attributeDict objectForKey:@"state"],[attributeDict objectForKey:@"zip"],[attributeDict objectForKey:@"status"],[attributeDict objectForKey:@"lastupdated"],[attributeDict objectForKey:@"deleted"]);
}
//like this way fetch all data and insert in db
}