Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从SQLite错误中检索数据_Android_Sqlite - Fatal编程技术网

Android 从SQLite错误中检索数据

Android 从SQLite错误中检索数据,android,sqlite,Android,Sqlite,我想知道我不能使用android代码从sqlite检索数据。在我添加两列(百分比和结果)之前,它像往常一样工作,但在我添加两列(百分比和结果)之后,当我单击按钮时,它显示错误。下面是我的代码: sqlite适配器-->已更新-已解决您还需要关闭光标 写入cursor.close()关闭数据库之前。同时关闭所有SimpleCursorAdapter的cursorAdapter2.close()cursorAdapter3.close()cursorAdapter4.close()cursorAda

我想知道我不能使用android代码从sqlite检索数据。在我添加两列(百分比和结果)之前,它像往常一样工作,但在我添加两列(百分比和结果)之后,当我单击按钮时,它显示错误。下面是我的代码:


sqlite适配器-->已更新-已解决您还需要关闭
光标


写入
cursor.close()关闭数据库之前。同时关闭所有
SimpleCursorAdapter的
cursorAdapter2.close()
cursorAdapter3.close()
cursorAdapter4.close()
cursorAdapter5.close()
cursorAdapter6.close()
cursorAdapter7.close()在销毁方法中

你的日志清楚地说

close() was never explicitly called on database 
'/data/data/kids.iq.kidsiqpicturesquestion/databases/KIDIQ' 
我认为问题在于,当您的活动被破坏时,您需要关闭数据库。尝试在活动中添加类似的内容:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (openHelper != null) {
        openHelper.close();
    }
    if (cdh != null) {
        cdh.close();
    }
}

信用卡到期

您需要在流程结束时关闭光标

public class scoreboard_fruit extends Activity{

//SQLite Method
private SQLiteAdapter mySQLiteAdapter;
SimpleCursorAdapter cursorAdapter;
Cursor cursor;

@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scoreboard_fruit);


ListView level = (ListView)findViewById(R.id.level);
ListView correct = (ListView)findViewById(R.id.correct);
ListView incorrect = (ListView)findViewById(R.id.incorrect);
ListView date = (ListView)findViewById(R.id.date);
ListView percentage = (ListView)findViewById(R.id.percentage);
ListView result = (ListView)findViewById(R.id.result);

mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();

cursor = mySQLiteAdapter.queue_FRUIT();
startManagingCursor(cursor);

String[] from2 = new String[]{SQLiteAdapter.KEY_LEVEL};
int[] to2 = new int[]{R.id.text};

String[] from3 = new String[]{SQLiteAdapter.KEY_CORRECT};
int[] to3 = new int[]{R.id.text};

String[] from4 = new String[]{SQLiteAdapter.KEY_INCORRECT};
int[] to4 = new int[]{R.id.text};

String[] from5 = new String[]{SQLiteAdapter.KEY_DATE};
int[] to5 = new int[]{R.id.text};

String[] from6 = new String[]{SQLiteAdapter.KEY_PERCENTAGE};
int[] to6 = new int[]{R.id.text};

String[] from7 = new String[]{SQLiteAdapter.KEY_RESULT};
int[] to7 = new int[]{R.id.text};

SimpleCursorAdapter cursorAdapter2 = new SimpleCursorAdapter(this, R.layout.row, cursor, from2, to2);
SimpleCursorAdapter cursorAdapter3 = new SimpleCursorAdapter(this, R.layout.row, cursor, from3, to3);
SimpleCursorAdapter cursorAdapter4 = new SimpleCursorAdapter(this, R.layout.row, cursor, from4, to4);
SimpleCursorAdapter cursorAdapter5 = new SimpleCursorAdapter(this, R.layout.row, cursor, from5, to5);
SimpleCursorAdapter cursorAdapter6 = new SimpleCursorAdapter(this, R.layout.row, cursor, from6, to6);
SimpleCursorAdapter cursorAdapter7 = new SimpleCursorAdapter(this, R.layout.row, cursor, from7, to7);


level.setAdapter(cursorAdapter2);
correct.setAdapter(cursorAdapter3);
incorrect.setAdapter(cursorAdapter4);
date.setAdapter(cursorAdapter5);
percentage.setAdapter(cursorAdapter6);
result.setAdapter(cursorAdapter7);

cursorAdapter2.close();
cursorAdapter3.close();
cursorAdapter4.close();
cursorAdapter5.close();
cursorAdapter6.close();
cursorAdapter7.close();
mySQLiteAdapter.close();
}
}

希望这有帮助

实际上您并没有关闭数据库

添加关闭数据库的方法

private void dbclose() {    
   sqLiteDatabase.close();
}

也只需关闭光标@SopheakVirak。@SopheakVirak能否将您的解决方案作为答案发布,并在其上打上绿色勾号,以便问题可以关闭,其他人可以从中受益。亲爱的GrlsHu,这不起作用。。出现相同的错误logcat:(在添加游标后编译.close();11-13 13:07:04.667:E/SQLiteDatabase(465):从未对数据库“/data/data/kids.iq.kidsiqpicturesquestion/databases/KIDIQ”11-13:07:04.667:E/SQLiteDatabase(465)显式调用close():android.database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭在此处打开的光标或数据库对象11-13 13:07:04.667:E/SQLiteDatabase(465):位于android.database.sqlite.SQLiteDatabase。(SQLiteDatabase.java:1980)@GrlsHu,仍然不起作用,并且出现了相同的错误logcat。关闭光标的位置?请更新问题中的代码。我告诉过你关闭所有光标,但你只关闭了数据库和适配器。只需像
cursorAdapter2.close();cursorAdapter3.close();cursorAdapter4.close();cursorAdapter5.close()那样编写;cursorAdapter6.close();cursorAdapter7.close();mySQLiteAdapter.close();cursor.close();
一次检查此链接,一次完成本教程
11-13 12:51:12.777: E/SQLiteDatabase(3902): close() was never explicitly called on database '/data/data/kids.iq.kidsiqpicturesquestion/databases/KIDIQ' 
11-13 12:51:12.777: E/SQLiteDatabase(3902): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1980)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:977)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:745)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:223)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at kids.iq.kidsiqpicturesquestion.SQLiteAdapter.openToRead(SQLiteAdapter.java:66)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at kids.iq.kidsiqpicturesquestion.scoreboard_fruit.onCreate(scoreboard_fruit.java:35)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.Activity.performCreate(Activity.java:4397)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.os.Looper.loop(Looper.java:132)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at android.app.ActivityThread.main(ActivityThread.java:4123)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at java.lang.reflect.Method.invoke(Method.java:491)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-13 12:51:12.777: E/SQLiteDatabase(3902):     at dalvik.system.NativeStart.main(Native Method)
close() was never explicitly called on database 
'/data/data/kids.iq.kidsiqpicturesquestion/databases/KIDIQ' 
@Override
protected void onDestroy() {
    super.onDestroy();
    if (openHelper != null) {
        openHelper.close();
    }
    if (cdh != null) {
        cdh.close();
    }
}
public class scoreboard_fruit extends Activity{

//SQLite Method
private SQLiteAdapter mySQLiteAdapter;
SimpleCursorAdapter cursorAdapter;
Cursor cursor;

@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scoreboard_fruit);


ListView level = (ListView)findViewById(R.id.level);
ListView correct = (ListView)findViewById(R.id.correct);
ListView incorrect = (ListView)findViewById(R.id.incorrect);
ListView date = (ListView)findViewById(R.id.date);
ListView percentage = (ListView)findViewById(R.id.percentage);
ListView result = (ListView)findViewById(R.id.result);

mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();

cursor = mySQLiteAdapter.queue_FRUIT();
startManagingCursor(cursor);

String[] from2 = new String[]{SQLiteAdapter.KEY_LEVEL};
int[] to2 = new int[]{R.id.text};

String[] from3 = new String[]{SQLiteAdapter.KEY_CORRECT};
int[] to3 = new int[]{R.id.text};

String[] from4 = new String[]{SQLiteAdapter.KEY_INCORRECT};
int[] to4 = new int[]{R.id.text};

String[] from5 = new String[]{SQLiteAdapter.KEY_DATE};
int[] to5 = new int[]{R.id.text};

String[] from6 = new String[]{SQLiteAdapter.KEY_PERCENTAGE};
int[] to6 = new int[]{R.id.text};

String[] from7 = new String[]{SQLiteAdapter.KEY_RESULT};
int[] to7 = new int[]{R.id.text};

SimpleCursorAdapter cursorAdapter2 = new SimpleCursorAdapter(this, R.layout.row, cursor, from2, to2);
SimpleCursorAdapter cursorAdapter3 = new SimpleCursorAdapter(this, R.layout.row, cursor, from3, to3);
SimpleCursorAdapter cursorAdapter4 = new SimpleCursorAdapter(this, R.layout.row, cursor, from4, to4);
SimpleCursorAdapter cursorAdapter5 = new SimpleCursorAdapter(this, R.layout.row, cursor, from5, to5);
SimpleCursorAdapter cursorAdapter6 = new SimpleCursorAdapter(this, R.layout.row, cursor, from6, to6);
SimpleCursorAdapter cursorAdapter7 = new SimpleCursorAdapter(this, R.layout.row, cursor, from7, to7);


level.setAdapter(cursorAdapter2);
correct.setAdapter(cursorAdapter3);
incorrect.setAdapter(cursorAdapter4);
date.setAdapter(cursorAdapter5);
percentage.setAdapter(cursorAdapter6);
result.setAdapter(cursorAdapter7);

cursorAdapter2.close();
cursorAdapter3.close();
cursorAdapter4.close();
cursorAdapter5.close();
cursorAdapter6.close();
cursorAdapter7.close();
mySQLiteAdapter.close();
}
}
private void dbclose() {    
   sqLiteDatabase.close();
}