sqlite open helper崩溃

时间:2013-06-15 22:12:19

标签: android sqliteopenhelper

我在使用SQLiteOpenHelper时遇到了一些问题。当我在onCreate中访问它的函数时,我的子类(ReviewOpenHelper)工作,但我读到你在使用sqlite时应该使用asynctask,所以我添加了一个asynctask的子类,但现在它崩溃了。对不起,如果这是一个非常简单的问题,我是Android的新手,我正在从教程中学习,所以我的基础很不稳定。这是代码(关于我使用asynctask和一般约定的任何其他建议也是受欢迎的,我很确定我没有按照我的意愿使用它):

package com.mycompany.csvtodrive;

import android.app.*;
import android.os.*;
import android.content.*;
import android.widget.*;
import android.view.*;
import java.util.*;

public class Review extends Activity implements View.OnClickListener
{

TextView key, value;
Button next;
Button show;
Button known;
Button delete;
Button edit;

ReviewOpenHelper db;
List<Set> sets;
Dialog d;

int card;
int size;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardview);

    key = (TextView) findViewById(R.id.tv_key);     
    next = (Button) findViewById(R.id.b_next);
    known = (Button) findViewById(R.id.b_known);
    delete = (Button) findViewById(R.id.b_delete);
    edit = (Button) findViewById(R.id.b_edit);
    show = (Button) findViewById(R.id.b_show);

    next.setOnClickListener(this);
    show.setOnClickListener(this);
    known.setOnClickListener(this);
    delete.setOnClickListener(this);
    edit.setOnClickListener(this);

    new Task().execute("string");

    d = new Dialog(this);
    d.setTitle("Value/Reason");
    value = new TextView(this);
    value.setPadding(20,0,20,20);
    card =-1;

}

public void onClick(View v){
    switch (v.getId()){
        case R.id.b_next:
            if(card==size-1 && size!=0)
                card=0;
            else
                card++;
            Set s = sets.get(card);
            key.setText(s.KEY);
            value.setText(s.VALUE);
            break;
        case R.id.b_show:
            d.setContentView(value);
            d.show();
            break;
        default:
            break;
    }
}

public class Task extends AsyncTask<String,Integer,String>{
    @Override
    protected String doInBackground(String... params){
        db = new ReviewOpenHelper(Review.this);
        card = db.getSetCount();
        sets = db.getAllSets();

        return "done";
    }
}
}

ReviewOpenHelper代码:

package com.mycompany.csvtodrive;

import android.app.*;
import android.widget.*;
import android.database.sqlite.*;
import android.database.SQLException;
import android.content.*;
import android.database.Cursor;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;


public class ReviewOpenHelper extends SQLiteOpenHelper
{

private static final int DATABASE_VERSION = 2;
private static final String TABLE_NAME = "data";
private static final String DATABASE_NAME = "ReviewDb";

private static final String KEY_ID = "id";
private static final String KEY_QUESTION = "review_key";
private static final String KEY_ANSWER = "review_value";
private static final String KEY_DATE = "review_date";

private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " (" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_QUESTION + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_DATE + ");";
private static final String TABLE_UPGRADE = "DROP_TABLE_IF_EXISTS " + TABLE_NAME;

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

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL(TABLE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL(TABLE_UPGRADE);
    onCreate(db);
}

public void addSet(Set s){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_QUESTION,s.KEY);
    values.put(KEY_ANSWER,s.VALUE);
    values.put(KEY_DATE,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

    db.insert(TABLE_NAME,null,values);
    db.close();
}

public Set getSet(int id){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NAME,new String[]{KEY_ID,KEY_QUESTION,KEY_ANSWER,KEY_DATE},KEY_ID+"=?",new String[]{String.valueOf(id)}, null, null, null, null);

    if(cursor!=null){
        cursor.moveToFirst();
    }

    Set s = new Set(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));

    return s;
}

public List<Set> getAllSets() {
    List<Set> setList = new ArrayList<Set>();

    String sql = "SELECT  * FROM " + TABLE_NAME;

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

    if (cursor.moveToFirst()) {
        do {
            Set s = new Set(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));
            setList.add(s);
        } while (cursor.moveToNext());
    }

    return setList;
}

public int getSetCount() {
    String sql = "SELECT  * FROM " + TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(sql, null);
    cursor.close();

    return cursor.getCount();
}

public int updateSet(Set s) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_QUESTION, s.KEY);
    values.put(KEY_ANSWER, s.VALUE);
    values.put(KEY_DATE, s.DATE);


    return db.update(TABLE_NAME, values, KEY_ID + " = ?",
        new String[] { String.valueOf(s.ID)});
}

public void deleteSet(Set s) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME, KEY_ID + " = ?",
        new String[] { String.valueOf(s.ID) });
    db.close();
}   

0 个答案:

没有答案