【问题标题】:AIR SQLite error with targetSdkVersion = 26targetSdkVersion = 26 的 AIR SQLite 错误
【发布时间】:2018-06-10 09:23:43
【问题描述】:

我有一个运行良好的移动应用 (android/ios),但 Google 要求开发人员现在使用 targetSdkVersion 26(我的到目前为止是 19)。 现在我使用 26,我有一个错误 #3125:

SQLError: 'Error #3125: Unable to open the database file.', details:'Connection closed.', operation:'open', detailID:'1001'

这是我打开数据库的代码:

static private function openDatabase(datebaseName:String) {

        sqlCon = new SQLConnection();           

        dbDir = File.documentsDirectory.resolvePath("ZANORG");
        dbDirFile = File.documentsDirectory.resolvePath("ZANORG/" + datebaseName + ".db");          

        if (dbDirFile.exists) {
            trace("File exists");
            sqlCon.addEventListener(SQLEvent.OPEN, onDatabaseOpen);
            sqlCon.open(dbDirFile);     // Error #3125 is here                      
        } else {
            trace("File does not exist");

            if (!dbDir.exists){
                trace("Folder doesn't exist > Create");
                try{
                    dbDir.createDirectory();
                }catch (error:Error){                       
                    trace("ERROR");
                    return;
                } 
            }
            sqlCon.addEventListener(SQLEvent.OPEN, onDatabaseCreationOpen);
            sqlCon.open(dbDirFile);             

            var strReq:String = "CREATE TABLE IF NOT EXISTS gamedata (Score INT)";              
            sqlquery(strReq);


            strReq = "INSERT INTO gamedata (Score) values(0)";
            sqlquery(strReq);
        }
    }

【问题讨论】:

  • 大概是这个:stackoverflow.com/questions/39103863/… 如果不是,那我还是觉得是权限问题而不是代码问题。
  • 您可能会发现此链接对SQLError #3125 有帮助,其中 这是预期的行为。数据库不会代表您创建文件夹。要解决此问题,您只需添加一些代码以确保该目录已经存在。例如:File.applicationStorageDirectory.resolvePath("db").createDirectory();
  • 我已经尝试过这些解决方案,但没有效果。我真的不明白为什么更改 targetSdkVersion 会导致这个错误!

标签: android sqlite actionscript-3 air


【解决方案1】:

找了几天终于找到了解决办法,就是Organis说的权限问题..

我为遇到同样问题的任何人发布我的解决方案,而不是调用我的 openDatabase 函数,我称之为 checkDatabase 函数:

private static function checkDatabase(databaseName:String){
        dbDirFile = File.documentsDirectory.resolvePath("ZANORG/" + databaseName + ".db");
        dbDir = File.documentsDirectory.resolvePath("ZANORG");

        if (File.permissionStatus != PermissionStatus.GRANTED){
            dbDir.addEventListener(PermissionEvent.PERMISSION_STATUS, function(e:PermissionEvent):void {
                if (e.status == PermissionStatus.GRANTED){
                    openDatabase(databaseName);
                }else{
                    // permission denied
                }
            });                  

            try {
                dbDir.requestPermission();
            }catch(e:Error){

                // another request is in progress

            }
        }else{
            openDatabase(databaseName);
        }       

    }   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多