0

这里的代码和故障在哪里......

package com.example.tasker;

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class TaskerDbHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "Manager";

    // tasks table name
    private static final String TABLE_TASKS = "tasks";

    // tasks Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_TASKNAME = "taskName";
    private static final String KEY_STATUS = "status";

    public TaskerDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_TASKS + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TASKNAME
                + " TEXT, " + KEY_STATUS + " INTEGER)";
        db.execSQL(sql);

        db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
        // Create tables again
        onCreate(db);
    }

    // Adding new task
    public void addTask(Task task) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_TASKNAME, task.getTaskName()); // task name
        // status of task- can be 0 for not done and 1 for done
        values.put(KEY_STATUS, task.getStatus());

        // Inserting Row
        db.insert(TABLE_TASKS, null, values);
        db.close(); // Closing database connection
    }

    public List<Task> getAllTasks() {
        List<Task> taskList = new ArrayList<Task>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_TASKS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list

                if(cursor.getCount()>0){
                    Task task = new Task();
                    task.setId(cursor.getInt(0));
                    task.setTaskName(cursor.getString(1));
                    task.setStatus(cursor.getInt(2));
                    // Adding contact to list
                    taskList.add(task);
                    cursor.close();
                }else {
                    cursor.close();
                }return taskList;
        }


//              Task task = new Task();
//              task.setId(cursor.getInt(0));
//              task.setTaskName(cursor.getString(1));
//              task.setStatus(cursor.getInt(2));       
                // Adding contact to list
//              taskList.add(task);
//              
//          } else if (cursor.moveToNext()){
//              cursor.close();
//          }


        // return task list
//      return taskList;





    public void updateTask(Task task) {
        // updating row
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_TASKNAME, task.getTaskName());
        values.put(KEY_STATUS, task.getStatus());
        db.update(TABLE_TASKS, values, KEY_ID + " = ?",new String[] {String.valueOf(task.getId())});
        db.close();
    }




}`

错误

07-17 15:49:51.889:E/AndroidRuntime(14450):原因:java.lang.IllegalStateException:尝试重新打开已经关闭的对象:SQLiteDatabase:/data/data/com.example.tasker/databases /经理

4

1 回答 1

0

从此代码中删除所有“db.close()”调用。您不必在每次使用后关闭数据库。

于 2013-07-17T14:26:26.210 回答