-2

Androidコードを使用してsqliteからデータを取得できないと思っていました。2 つの列 (パーセンテージと結果) を追加する前は通常どおり動作しますが、2 つの列 (パーセンテージと結果) を追加した後、ボタンをクリックするとエラーが表示されます。以下は私のコードです:

sqlite アダプター -->更新 - 解決済み<--:

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

4 に答える 4

0

実際には、データベースを閉じていません...

データベースを閉じるメソッドを追加します

private void dbclose() {    
   sqLiteDatabase.close();
}
于 2013-11-13T06:26:24.907 に答える
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();
}
}

お役に立てれば !!!

于 2013-11-13T06:17:37.217 に答える