Android SQLite 在模拟器中崩溃,但在手机中却没有?

发布于 2024-10-15 18:49:59 字数 977 浏览 8 评论 0原文

我遇到一个有趣的问题,我的代码在手机上运行良好,但在模拟器上运行时,一旦应用程序尝试从数据库获取值,我就会崩溃。

这是导致它崩溃的 DB Helper 函数

/** Used to retrieve our lowest stored val */
public int GetLowestVal(){

    // Get our databases results
    Cursor result = null;
    try { 
        // Get our databases results
        result = super.getAll();

    } catch( SQLException anyDbError ) { 
        // Errors? TODO
    } finally { 
        // Finally TODO
    }


    // Setup initial lowest val
    int lowestVal = 0;

    // Move through results and compare them
    while( result.moveToNext() ){
        // Get our place from the DB
        int index = Integer.parseInt(result.getString(1).trim());

        // Last index should be lowest
        if ( index == 10){
            lowestVal = Integer.parseInt(result.getString(3).trim());
        }   
    }

    // Return our value
    return lowestVal;

}

,它抛出的错误是:

递归调用 getWriteableDatabase()。

I'm having an interesting issue where my code runs fine on my phone, but when running on the emulator i get a crash as soon as the app tries to get a value from the database.

Here is the DB Helper function which is crashing it

/** Used to retrieve our lowest stored val */
public int GetLowestVal(){

    // Get our databases results
    Cursor result = null;
    try { 
        // Get our databases results
        result = super.getAll();

    } catch( SQLException anyDbError ) { 
        // Errors? TODO
    } finally { 
        // Finally TODO
    }


    // Setup initial lowest val
    int lowestVal = 0;

    // Move through results and compare them
    while( result.moveToNext() ){
        // Get our place from the DB
        int index = Integer.parseInt(result.getString(1).trim());

        // Last index should be lowest
        if ( index == 10){
            lowestVal = Integer.parseInt(result.getString(3).trim());
        }   
    }

    // Return our value
    return lowestVal;

}

And the error it throws is:

getWriteableDatabase() was called recursively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-10-22 18:49:59

抱歉,问题是我仍在尝试编辑 onCreate() 方法中的表,这会导致递归调用 getWrteableDatabase()

它在模拟器上不起作用但在手机上起作用的原因是,在将任何内容添加到 onCreate 之前就已经在手机上创建了数据库,但将其部署到其他设备上导致了崩溃。

Sorry about that, the problem was that i was trying to edit the table still in the onCreate() method which causes getWrteableDatabase() to be called recursively.

The reason it wasn't working on the emulator but worked on the phone was because the database had been created on the phone before anything was added to onCreate, but deploying it on other devices caused the crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文