从SQLite错误中检索数据

时间:2013-11-13 05:54:08

标签: android sqlite

我想知道我无法使用android代码从sqlite中检索数据。在我添加两列(百分比和结果)之前,它像往常一样工作但在我添加两列(百分比和结果)之后,当我点击按钮时出现错误。贝娄是我的代码:

sqlite adapter - >已更新 - Soloved< - :

public class SQLiteAdapter{

//DB NAME
public static final String DATABASE_NAME = "KIDIQ";
//3 TABLE
public static final String TABLE_FRUIT = "FRUIT";

//DB VERSION
public static final int DATABASE_VERSION = 1;

//Common column names
public static final String KEY_ID = "_id";
public static final String KEY_LEVEL = "LEVEL";
public static final String KEY_CORRECT = "CORRECT";
public static final String KEY_INCORRECT = "INCORRECT";
public static final String KEY_DATE = "DATE";
public static final String KEY_PERCENTAGE = "PERCENTAGE";
public static final String KEY_RESULT = "RESULT";

// Table Create Statements
// FRUIT table create statement
private static final String CREATE_TABLE_FRUIT = "CREATE TABLE "
        + TABLE_FRUIT + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_LEVEL
        + " TEXT NOT NULL, " + KEY_CORRECT + " TEXT NOT NULL, " + KEY_INCORRECT
        + " TEXT NOT NULL, " + KEY_DATE + " TEXT NOT NULL, " + KEY_PERCENTAGE 
        + " TEXT NOT NULL, " + KEY_RESULT + " TEXT NOT NULL);"


private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public SQLiteAdapter(Context c){
      context = c;
     }

public SQLiteAdapter openToRead() throws android.database.SQLException
{
    sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    return this;
}

public SQLiteAdapter openToWrite() throws android.database.SQLException
{
    sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    return this; 
}

public void close()
{
    sqLiteHelper.close();
}

public long insert_FRUIT(String level, String correct, String incorrect, String date, String percentage, String result)
{
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_LEVEL, level);
    contentValues.put(KEY_CORRECT, correct);
    contentValues.put(KEY_INCORRECT, incorrect);
    contentValues.put(KEY_DATE, date);
    contentValues.put(KEY_PERCENTAGE, percentage);
    contentValues.put(KEY_RESULT, result);
    return sqLiteDatabase.insert(TABLE_FRUIT, null, contentValues);
}

public int delete_FRUIT()
{
    return sqLiteDatabase.delete(TABLE_FRUIT, null, null);
}

//Retrieve data from FRUIT TABLE
public Cursor queue_FRUIT()
{
    String[] columns = new String[]{KEY_ID, KEY_LEVEL, KEY_CORRECT, KEY_INCORRECT, KEY_DATE, KEY_PERCENTAGE, KEY_RESULT};
    Cursor cursor = sqLiteDatabase.query(TABLE_FRUIT, columns, null, null, null, null, null, KEY_DATE+" DESC");
    return cursor;
}

public class SQLiteHelper extends SQLiteOpenHelper
{
    public SQLiteHelper(Context context, String name, CursorFactory factory, int version)
    {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_TABLE_FRUIT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // TODO Auto-generated method stub
            //UPDATED TO SOVE PROBLEM
            @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // TODO Auto-generated method stub
        // on upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FRUIT);

        // create new tables
        onCreate(db);

    }

    }
}
}

活动代码:

public class scoreboard_fruit extends Activity{

//SQLite Method
private SQLiteAdapter mySQLiteAdapter;
SimpleCursorAdapter cursorAdapter2;
SimpleCursorAdapter cursorAdapter3;
SimpleCursorAdapter cursorAdapter4;
SimpleCursorAdapter cursorAdapter5;
SimpleCursorAdapter cursorAdapter6;
SimpleCursorAdapter cursorAdapter7;

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};

cursorAdapter2 = new SimpleCursorAdapter(this, R.layout.row, cursor, from2, to2);
cursorAdapter3 = new SimpleCursorAdapter(this, R.layout.row, cursor, from3, to3);
cursorAdapter4 = new SimpleCursorAdapter(this, R.layout.row, cursor, from4, to4);
cursorAdapter5 = new SimpleCursorAdapter(this, R.layout.row, cursor, from5, to5);
cursorAdapter6 = new SimpleCursorAdapter(this, R.layout.row, cursor, from6, to6);
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);

cursor.close();
mySQLiteAdapter.close();
}
}

布局:

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip"
    android:gravity="center">
    <ListView
        android:id="@+id/level"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#D29851"
        android:textSize="16sp"
        android:textColor="#FFF"
        android:paddingLeft="10dp"/>
    <ListView
        android:id="@+id/correct"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#D29851"
        android:textSize="16sp"
        android:textColor="#0022FF"
        android:paddingLeft="10dp"
        android:layout_marginLeft="1dp"/>
    <ListView
        android:id="@+id/incorrect"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#D29851"
        android:textSize="16sp"
        android:textColor="#DC0303"
        android:paddingLeft="10dp"
        android:layout_marginLeft="1dp"/>
    <ListView
        android:id="@+id/date"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#D29851"
        android:textSize="16sp"
        android:textColor="#FFF"
        android:paddingLeft="10dp"
        android:layout_marginLeft="1dp"/>
    <ListView
        android:id="@+id/percentage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#D29851"
        android:textSize="16sp"
        android:textColor="#FFF"
        android:paddingLeft="10dp"
        android:layout_marginLeft="1dp"/>
    <ListView
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#D29851"
        android:textSize="16sp"
        android:textColor="#FFF"
        android:paddingLeft="10dp"
        android:layout_marginLeft="1dp"/>
</TableRow>

布局row.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textColor="#FFF" />

的logcat:

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)

我在代码中找不到错误但是当我遵守它出现错误时。请帮助我。提前谢谢。

* * 现在,我在public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)上更新后解决了。

谢谢大家的帮助。

4 个答案:

答案 0 :(得分:1)

你的日志清楚地说,

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();
    }
}

积分here

答案 1 :(得分:0)

您还需要关闭Cursor

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

答案 2 :(得分:0)

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

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();
}
}

希望这有帮助!!!

答案 3 :(得分:0)

其实你没有关闭数据库...

添加关闭数据库的方法

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