Android SQLiteException崩溃

时间:2017-08-05 02:02:56

标签: android sqlite

崩溃报告中出现错误android.database.sqlite.SQLiteException。但是我的应用程序通过自己的手机和模拟器正常工作。

java.lang.RuntimeException: 

  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2319)

  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2379)

  at android.app.ActivityThread.access$800 (ActivityThread.java:147)

  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1284)

  at android.os.Handler.dispatchMessage (Handler.java:102)

  at android.os.Looper.loop (Looper.java:135)

  at android.app.ActivityThread.main (ActivityThread.java:5257)

  at java.lang.reflect.Method.invoke (Method.java)

  at java.lang.reflect.Method.invoke (Method.java:372)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:899)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: 

  at android.database.sqlite.SQLiteConnection.nativePrepareStatement (SQLiteConnection.java)

  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement (SQLiteConnection.java:889)

  at android.database.sqlite.SQLiteConnection.prepare (SQLiteConnection.java:500)

  at android.database.sqlite.SQLiteSession.prepare (SQLiteSession.java:588)

  at android.database.sqlite.SQLiteProgram.<init> (SQLiteProgram.java:58)

  at android.database.sqlite.SQLiteStatement.<init> (SQLiteStatement.java:31)

  at android.database.sqlite.SQLiteDatabase.executeSql (SQLiteDatabase.java:1676)

  at android.database.sqlite.SQLiteDatabase.execSQL (SQLiteDatabase.java:1607)

  at DictionaryDB2.addBook (DictionaryDB2.java)

  at <OR>.getBookmarkedWords (DictionaryDB2.java)

  at <OR>.deleteEntry (DictionaryDB2.java)

  at <OR>.addHistory (DictionaryDB2.java)

  at Word.onCreate (Word.java)

  at android.app.Activity.performCreate (Activity.java:6018)

  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1105)

  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2272)

数据库初始化程序:

public class DatabaseInitializer2 extends SQLiteOpenHelper {

    private static String DB_NAME = "bookmark";
    private static final int DB_VERSION = 59;

    private static final String TAG = DatabaseInitializer2.class.getName();

    public DatabaseInitializer2(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_BOOK_TABLE = "CREATE TABLE IF NOT EXISTS bookmark ( " +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "en_word TEXT, "+
                "bn_word TEXT, "+
                "status TEXT, "+
                "user_created TEXT )";

        String CREATE_HISTORY_TABLE = "CREATE TABLE history ( " +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "en_word TEXT, "+
                "bn_word TEXT, "+
                "status TEXT, "+
                "user_created TEXT, "+
                "UNIQUE (en_word) ON CONFLICT REPLACE )";

        db.execSQL(CREATE_BOOK_TABLE);

        db.execSQL(CREATE_HISTORY_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        this.onCreate(db);
    }
}

这是DictionaryDB2.addBook方法:

public void addBook(String englishWord, String banglaWord) {
        SQLiteDatabase db = initializer.getWritableDatabase();

        String sql = "INSERT INTO " + BOOK_NAME + " (" + ENGLISH +
                ", " + BANGLA + ") VALUES ('" + englishWord +
                "', '" + banglaWord + "') ";
        db.execSQL(sql);
    }

这是我的getBookmarkedWords:

public List<Bean> getBookmarkedWords() {
        SQLiteDatabase db = initializer.getReadableDatabase();

        String sql = "SELECT * FROM " + BOOK_NAME + " ORDER BY " + ID + " DESC ";

        Cursor cursor = db.rawQuery(sql, null);

        List<Bean> wordList = new ArrayList<Bean>();
        while(cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String english = cursor.getString(1);
            String bangla = cursor.getString(2);
            String status = cursor.getString(3);
            wordList.add(new Bean(id, english, bangla, status));
        }

        cursor.close();
        db.close();
        return wordList;
    }

Stackoverflow不允许我添加更多代码。有人知道我的代码出了什么问题吗?

2 个答案:

答案 0 :(得分:-1)

我建议你使用带有contentsvalues的方法insert而不是execsql。

/ <强> 修改 /

public DatabaseInitializer2(Context context, String name, 
SQLiteDatabase.CursorFactory 
factory, int version) {
    super(context, name, factory, version);
}

尝试使用该构造函数而不是您的构造函数。我没有检测到其余代码中的任何错误。如果错误仍然存​​在,请发布从SQLiteOpenHelper扩展的类,以便我们也可以检查它

答案 1 :(得分:-2)

您需要添加

cursor.moveToFirst();

之前

 while(cursor.moveToNext()) {
        int id = cursor.getInt(0);
        String english = cursor.getString(1);
        String bangla = cursor.getString(2);
        String status = cursor.getString(3);
        wordList.add(new Bean(id, english, bangla, status));
    }

并将以上行更改为:

 while(!cursor.isAfterLast()) {
        int id = cursor.getInt(0);
        String english = cursor.getString(1);
        String bangla = cursor.getString(2);
        String status = cursor.getString(3);
        wordList.add(new Bean(id, english, bangla, status));
        cursor.moveToNext();
    }