You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-native-SQLite-storage适配Android10+批量更新异常问题求助

问题:react-native-sqlite-storage批量更新在不同Android版本的兼容性问题

依赖版本

{
  "react": "17.0.2",
  "react-native": "0.66.4",
  "react-native-sqlite-storage": "^6.0.1"
}

已配置的react-native.config.js内容

module.exports = {
    dependencies: {
        "react-native-sqlite-storage": {
            platforms: {
                android: {
                    sourceDir: "../node_modules/react-native-sqlite-storage/platforms/android-native",
                    packageImportPath: "import io.liteglue.SQLitePluginPackage;",
                    packageInstance: "new SQLitePluginPackage()"
                }
            }
        }
    }
};

问题现象

  • 移除上述react-native.config.js配置后,Android 11及以上版本能正常执行批量更新操作
  • 保留配置时,只有Android 10及以下版本运行正常,Android 11+出现异常
  • 使用的批量更新SQL语句:
INSERT INTO Maintable SELECT * FROM Landingtable WHERE TRUE ON CONFLICT(Symbol) DO UPDATE SET Name = excluded.Name, Symbol = excluded.Symbol

解决方法尝试

1. 按Android版本动态配置插件实现

问题核心是android-native(LiteGlue)实现对Android 11+的兼容性不足,可针对不同版本切换插件实现:

  • Android 10及以下:保留原android-native配置
  • Android 11及以上:使用默认Android实现(platforms/android)

修改react-native.config.js如下(需验证版本判断逻辑):

module.exports = {
  dependencies: {
    "react-native-sqlite-storage": {
      platforms: {
        android: {
          sourceDir: () => {
            // 获取当前编译的Android SDK版本
            const { getProjectConfig } = require('react-native/local-cli/core/config');
            const projectConfig = getProjectConfig();
            const compileSdkVersion = projectConfig.android.compileSdkVersion;
            return parseInt(compileSdkVersion) >= 30 
              ? "../node_modules/react-native-sqlite-storage/platforms/android" 
              : "../node_modules/react-native-sqlite-storage/platforms/android-native";
          },
          packageImportPath: () => {
            const { getProjectConfig } = require('react-native/local-cli/core/config');
            const projectConfig = getProjectConfig();
            const compileSdkVersion = projectConfig.android.compileSdkVersion;
            return parseInt(compileSdkVersion) >= 30 
              ? "import com.reactnativecommunity.sqlite.SQLitePluginPackage;" 
              : "import io.liteglue.SQLitePluginPackage;";
          },
          packageInstance: () => {
            const { getProjectConfig } = require('react-native/local-cli/core/config');
            const projectConfig = getProjectConfig();
            const compileSdkVersion = projectConfig.android.compileSdkVersion;
            return parseInt(compileSdkVersion) >= 30 
              ? "new SQLitePluginPackage()" 
              : "new io.liteglue.SQLitePluginPackage()";
          }
        }
      }
    }
  }
};

2. 替换SQL语句适配插件

部分旧版LiteGlue对ON CONFLICT(UPSERT)语法支持不完善,可尝试用REPLACE INTO替代(注意:REPLACE会删除旧行再插入,可能影响自增ID等字段):

REPLACE INTO Maintable SELECT * FROM Landingtable WHERE TRUE

如果需要保留原有行的其他字段,也可以拆分为先查询冲突项,再分别执行UPDATE和INSERT,但效率会有所降低。

3. 升级插件版本

尝试将react-native-sqlite-storage升级到最新稳定版,新版本可能修复了Android 11+的兼容性问题。

4. 检查存储权限

确保Android 11+应用使用私有目录存储SQLite数据库,避免使用外部存储路径;若必须用外部存储,需适配MANAGE_EXTERNAL_STORAGE权限(不推荐优先使用)。

内容的提问来源于stack exchange,提问作者Raj-p.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 00:51:19