SQLITE数据库是加密的,或者从谷歌驱动器下载时不是数据库,它在 Flutter

6tqwzwtp  于 12个月前  发布在  SQLite
关注(0)|答案(1)|浏览(179)

我正在开发一个使用SQLite数据库的Flutter应用程序。用户可以使用公共共享链接从Google Drive下载数据库。如果有新版本的数据库可用,将自动替换现有数据库。但是,下载时遇到问题。出现此错误“

file is encrypted or is not a database
E/DefaultDatabaseErrorHandler( 5315): !@ Corruption reported by sqlite (26) on database: /data/user/0/com.example.replace_db/databases/contacts.db
E/DefaultDatabaseErrorHandler( 5315): !@ dbObj has been closed
I/flutter ( 5315): Database updated to version 2."

字符串
我的代码是,

Future<void> checkAndReplaceDatabase() async {
   String databaseUrl='https://drive.google.com/file/d/1Qt4xigUU3d7tO-sdyZVZyQKei9l6AvBz';
   // Define the expected database version.
  final int expectedVersion = 2; // Change this to your desired version.

  // Get a reference to the device's database file.
  String databasesPath = await getDatabasesPath();
  String path = join(databasesPath, 'contacts.db');

  // Open the database.
  Database database = await openDatabase(path);

  // Get the current database version.
  int currentVersion = await database.getVersion();

  if (currentVersion < expectedVersion) {
    // New version is available. Download and replace the database.

    // Close the database before replacing it.
    await database.close();

    // Download the new database from the specified URL.
    var response = await http.get(Uri.parse(databaseUrl));

    if (response.statusCode == 200) {
      // Save the downloaded file to the device's database path.
      File newDatabaseFile = File(path);
      await newDatabaseFile.writeAsBytes(response.bodyBytes);

      // Open the new database.
      Database newDatabase = await openDatabase(path);

      // Set the new version for the database.
      await newDatabase.setVersion(expectedVersion);

      // Close the new database.
      await newDatabase.close();

      print('Database updated to version $expectedVersion.');
    } else {
      print('Failed to download the new database.');
    }
  } else {
    // The database is up to date.
    print('Database is already up to date (version $currentVersion).');
  }
}


请帮帮我

yi0zb3m4

yi0zb3m41#

函数运行正常,问题是url返回一个HTML文档,看起来像是以字节的形式写入contacts.db的。
尝试更改URL以直接从链接下载,如下所示:将file/d/替换为uc?id=,然后将&export=download放在末尾。

'https://drive.google.com/file/d/1Qt4xigUU3d7tO-sdyZVZyQKei9l6AvBz';
// should look like this
'https://drive.google.com/uc?id=1Qt4xigUU3d7tO-sdyZVZyQKei9l6AvBz&export=download'

字符串
这应该现在工作.但是,这取决于服务提供商和这些链接可能会打破,如果它不是推荐的方式.寻求文档.

相关问题