执行SQLite查询时出现Android错误

时间:2015-08-18 13:40:08

标签: android sqlite

我想从现有的SQLite数据库中读取数据。

DataHelper.java

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class DataHelper {
private static final String NAMA_DB = "db1";
private static final int VERSI_DB = 1;
private static final String NAMA_TABEL = "table1";

private Context context;
private SQLiteDatabase db;

private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + NAMA_TABEL + "(name) values (?)";

public DataHelper(Context context){
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
    this.insertStmt = this.db.compileStatement(INSERT);
}

public long insert(String name){
    this.insertStmt.bindString(1, name);
    return this.insertStmt.executeInsert();
}

public void deleteAll(){
    this.db.delete(NAMA_TABEL, null, null);
}

public List<String> selectAll(){
    List<String> list = new ArrayList<String>();
    list.add("afdasasdfsad");
    //Cursor cursor = this.db.query(NAMA_TABEL, new String[]{ "name" }, null,  null,  null,  null, "name desc");
    Cursor cursor =  db.rawQuery( "select * from table1", null );
    /*
    if(cursor.moveToFirst()){
        do{
            list.add(cursor.getString(0));
        } while (cursor.moveToNext());
    }
    if(cursor != null && !cursor.isClosed()){
        cursor.close();
    }*/


    return list;
}

private static class OpenHelper extends SQLiteOpenHelper{
    OpenHelper(Context context){
        super(context, NAMA_DB, null, VERSI_DB);
    }
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE " + NAMA_TABEL + "(id INTEGER PRIMARU KEY, name TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        Log.w("Contoh","upgrade database. Tabel akan dihapus dan dibuat ulang");
        db.execSQL("DROP TABLE IF EXISTS " + NAMA_TABEL);
        onCreate(db);
    }
}

我调用从数据库中检索数据的函数的部分代码

DataHelper dh = new DataHelper(getActivity());
List<String> results = dh.selectAll();
StringBuilder sb = new StringBuilder();
for(String r : results){
  sb.append("Hasil = " + r + "\n");
}
String res = sb.toString();
txvMain.setText(res);

这是logcat

08-18 20:20:47.991: E/AndroidRuntime(1248): FATAL EXCEPTION: main
08-18 20:20:47.991: E/AndroidRuntime(1248): android.database.sqlite.SQLiteException: no such table: table1: , while compiling: select * from table1
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at com.example.app02.DataHelper.selectAll(DataHelper.java:44)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at com.example.app02.MainActivity$PlaceholderFragment$1.onTextChanged(MainActivity.java:156)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.widget.TextView.sendOnTextChanged(TextView.java:7634)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.widget.TextView.handleTextChanged(TextView.java:7681)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8044)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:218)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.widget.TextView.doKeyDown(TextView.java:5445)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.widget.TextView.onKeyDown(TextView.java:5267)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.KeyEvent.dispatch(KeyEvent.java:2551)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.View.dispatchKeyEvent(View.java:5500)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.os.Looper.loop(Looper.java:137)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at android.app.ActivityThread.main(ActivityThread.java:4424)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at java.lang.reflect.Method.invoke(Method.java:511)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-18 20:20:47.991: E/AndroidRuntime(1248):     at dalvik.system.NativeStart.main(Native Method)
08-18 20:20:48.021: D/dalvikvm(1248): GC_CONCURRENT freed 236K, 4% free 9300K/9607K, paused 4ms+6ms
08-18 20:20:48.051: E/SQLiteDatabase(1248): close() was never explicitly called on database '/data/data/com.example.app02/databases/db1' 
08-18 20:20:48.051: E/SQLiteDatabase(1248): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at com.example.app02.DataHelper.<init>(DataHelper.java:27)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at com.example.app02.MainActivity$PlaceholderFragment$1.onTextChanged(MainActivity.java:155)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.widget.TextView.sendOnTextChanged(TextView.java:7634)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.widget.TextView.handleTextChanged(TextView.java:7681)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8044)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:218)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.widget.TextView.doKeyDown(TextView.java:5445)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.widget.TextView.onKeyDown(TextView.java:5267)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.KeyEvent.dispatch(KeyEvent.java:2551)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.View.dispatchKeyEvent(View.java:5500)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.os.Looper.loop(Looper.java:137)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at android.app.ActivityThread.main(ActivityThread.java:4424)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at java.lang.reflect.Method.invoke(Method.java:511)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-18 20:20:48.051: E/SQLiteDatabase(1248):     at dalvik.system.NativeStart.main(Native Method)

我在Firefox上使用SQLite Manage创建了数据库,并将该文件复制到项目的assets文件夹中。

如何解决此错误?

2 个答案:

答案 0 :(得分:0)

而不是“table1”使用另一个表名并清除应用程序数据后尝试运行您的应用程序

答案 1 :(得分:0)

table1根本不存在,或者您的数据库已过时。

每次更改数据库架构时,请更改数据库版本:

private static final int VERSI_DB = 1;

这将导致运行onUpgrade()逻辑以更新数据库。