我们如何从url下载sqlite数据库并将其作为sqlite数据库添加到我们的应用程序中?

时间:2022-11-19 15:06:26

I used sqlite database in my application.I want to synchronize this database with my mysql server.But i think it is easy to replace exiting database with new database in my application.So,It will also solve data import export problem.But i don't know how download .sqlite file from my url and add it into in my application bundle.

我在我的应用程序中使用了sqlite数据库。我想将这个数据库与我的mysql服务器同步。但我认为在我的应用程序中用新数据库替换现有的数据库很容易。所以,它也会解决数据导入导出问题。但是我不知道。我不知道如何从我的网址下载.sqlite文件并将其添加到我的应用程序包中。

In simple way.I want to add file in Xcode Resource folder at runtime.I don't how to do it. Please help me if anybody have idea.

以简单的方式。我想在运行时在Xcode Resource文件夹中添加文件。我不知道怎么做。如果有人有想法,请帮助我。

Thanks In Advance.

提前致谢。

2 个解决方案

#1


6  

Try following code :

请尝试以下代码:

NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]];

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"];

[dbFile writeToFile:filePath atomically:YES];

Now you can use this database file.

现在您可以使用此数据库文件。

#2


0  

To add downloaded database to your application as sqlite database try following code

要将下载的数据库作为sqlite数据库添加到应用程序,请尝试以下代码

NSString *dbfilepath = [FileUtils documentsDirectoryPathForResource:[NSString stringWithFormat:@"%@.db", @"downloadedDatabase"]];
NSData *dbData = [NSData dataWithContentsOfFile:dbfilepath];

NSString *filePath = [[FileUtils documentsDirectoryPath] stringByAppendingPathComponent:@"Database.sqlite"];

[dbData writeToFile:filePath atomically:YES];

Now your application database is ready with imported records.

现在您的应用程序数据库已准备好导入记录。

#1


6  

Try following code :

请尝试以下代码:

NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]];

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"];

[dbFile writeToFile:filePath atomically:YES];

Now you can use this database file.

现在您可以使用此数据库文件。

#2


0  

To add downloaded database to your application as sqlite database try following code

要将下载的数据库作为sqlite数据库添加到应用程序,请尝试以下代码

NSString *dbfilepath = [FileUtils documentsDirectoryPathForResource:[NSString stringWithFormat:@"%@.db", @"downloadedDatabase"]];
NSData *dbData = [NSData dataWithContentsOfFile:dbfilepath];

NSString *filePath = [[FileUtils documentsDirectoryPath] stringByAppendingPathComponent:@"Database.sqlite"];

[dbData writeToFile:filePath atomically:YES];

Now your application database is ready with imported records.

现在您的应用程序数据库已准备好导入记录。