SQLite Android 问题

发布于 2024-10-31 04:54:44 字数 1399 浏览 2 评论 0原文

我正在使用 SQLite 为我的 Android 应用程序 (Java) 制作本地高分表。

由于某种原因,应用程序在尝试向表中添加新的高分时崩溃。这是相关代码:

private static String HIGHSCORES_TABLE_NAME = "SCORES_TABLE";
private SQLiteDatabase highScoresDB = null;
private Cursor cursor = null;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    try
    {
        highScoresDB = openOrCreateDatabase("ScoresDatabase", MODE_PRIVATE, null);
        createTable();
        lookupData();
    }
    catch (SQLiteException se)
    {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    }
    finally
    {

        if (highScoresDB != null)
                           highScoresDB.execSQL("DELETE FROM " + HIGHSCORES_TABLE_NAME);
        highScoresDB.close();
    }
}

private void createTable() {
    highScoresDB.execSQL("CREATE TABLE IF NOT EXISTS " + HIGHSCORES_TABLE_NAME + " (SCORE INT(3));");
}

private void insertData() {
    highScoresDB.execSQL("INSERT INTO " + HIGHSCORES_TABLE_NAME + " Values ("+ highscore +");");
}

private void lookupData() {
    cursor = highScoresDB.rawQuery("SELECT SCORE FROM " + HIGHSCORES_TABLE_NAME, null);
    if (cursor.moveToLast()) {
        highscore = cursor.getInt(cursor.getColumnIndex("SCORE"));
    }
    cursor.close();
}

public void restart() {
    insertData();
}

当我查找高分时,我只想要最近的分数,所以我使用 moveToLast()。

I am making a local high scores table using SQLite for my Android application (Java).

For some reason, the application crashes while trying to add a new high score to the table. Here is the relevant code:

private static String HIGHSCORES_TABLE_NAME = "SCORES_TABLE";
private SQLiteDatabase highScoresDB = null;
private Cursor cursor = null;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    try
    {
        highScoresDB = openOrCreateDatabase("ScoresDatabase", MODE_PRIVATE, null);
        createTable();
        lookupData();
    }
    catch (SQLiteException se)
    {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    }
    finally
    {

        if (highScoresDB != null)
                           highScoresDB.execSQL("DELETE FROM " + HIGHSCORES_TABLE_NAME);
        highScoresDB.close();
    }
}

private void createTable() {
    highScoresDB.execSQL("CREATE TABLE IF NOT EXISTS " + HIGHSCORES_TABLE_NAME + " (SCORE INT(3));");
}

private void insertData() {
    highScoresDB.execSQL("INSERT INTO " + HIGHSCORES_TABLE_NAME + " Values ("+ highscore +");");
}

private void lookupData() {
    cursor = highScoresDB.rawQuery("SELECT SCORE FROM " + HIGHSCORES_TABLE_NAME, null);
    if (cursor.moveToLast()) {
        highscore = cursor.getInt(cursor.getColumnIndex("SCORE"));
    }
    cursor.close();
}

public void restart() {
    insertData();
}

When I look up the high score, I only want the most recent one so I use moveToLast().

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

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

发布评论

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

评论(3

美人迟暮 2024-11-07 04:54:44

我发现“cursor.moveToLast()”似乎无法正常工作。它确实移动了光标中的位置,但当您尝试读取该记录时会引发异常。

这有效:

cursor.moveToPosition(cursor.getCount()-1)

I found that "cursor.moveToLast()" does not seem to work correctly. It does move the position in the cursor, but throws an exception when you attempt to read that record.

This works:

cursor.moveToPosition(cursor.getCount()-1)

ˇ宁静的妩媚 2024-11-07 04:54:44

导致其崩溃的异常是什么,您何时插入新行?您是否在插入行之前打开数据库,因为在 create 方法的finally 块中调用了 Close() 。

如果这是您要存储在数据库中的唯一信息,请考虑使用共享首选项。
这个链接有一个如何使用它的演示 -
共享偏好设置演示

What is the exception that causes it to crash, When are you inserting a new row ? Are you opening the database before inserting a row , as Close() is being called on in the finally block of the create method.

If this is the only information you are going to store in the database , consider using Shared Preferences.
This link has a demo of how to use it -
Shared preferences demo

歌枕肩 2024-11-07 04:54:44

不要像您那样执行语句,而是使用 SQLite 对象类中包含的一些提供的方法:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

这使得您的代码不易出现 SQL 打字错误或小错误。

另请发布您的 logcat。

Instead of doing statements like you are doing use some of the provided methods that are included in the SQLite Object class:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

This makes your code less error prone to SQL Typos or small errors.

Also please post your logcat.

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