与今天的扩展共享 SQLite 不起作用

Share SQLite with today extension not working

我正在开发小部件应用程序。我对 Today Extension 完全陌生。我想在应用程序和今日扩展程序之间共享数据。我创建了一个数据库。我在该数据库中有 4 个表。我找到这段代码来为应用程序组创建数据库:

NSString *appGroupId = @"group.appname";
    NSURL *appGroupDirectoryPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroupId];
    dataBaseURL = [appGroupDirectoryPath  URLByAppendingPathComponent:@"database.sqlite"];
    NSLog(@"dataBaseURL %@", dataBaseURL);

成功了。我的 SQLite 文件已创建。但它没有显示我的表格。它显示空数据库。我对此一无所知。请帮忙。

如果您正在创建新数据库,那么它显然是空的。

首先您可以使用 objective c 中的表创建数据库。无需从 xcode 外部创建数据库并将其拖放到 xcode。你可以参考这个techotopia tutorial

现在,如果您通过拖放将数据库放入主包中,那么您就可以访问它,

 NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"myDbName" ofType:@"sqlite"];

通过这种方式你可以得到你的数据库路径。这里的 pathforresource 是你的数据库名称,我认为你的情况是 databaseoftypeextention,我认为你的情况是 sqlite

现在如果你想在 url 中获得字符串形式的路径,那么你可以在 url 中将其转换为

  NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

  or

  NSURL *fileURL = [NSURL fileURLWithPath:filePath];

希望这会有所帮助:)