错误代码:1(SQLITE_ERROR)Android

时间:2016-06-16 19:11:43

标签: android sql sqlite

我不明白这个错误:

引起:SQL(查询)错误或缺少数据库。                                                                             (没有这样的表:评估(代码1):,编译时:SELECT _id,evaluation_subject,weight,mark,course_id FROM evaluation)

EvaluationDAO.java

    import android.app.ListFragment;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.util.Log;

    import java.util.ArrayList;
    import java.util.List;
public class EvaluationDAO {

    public static final String TAG = "EvaluationDAO";

    private Context context;

    private SQLiteDatabase database;
    private DBHelper dbHelper;
    private String[] allColumns = { DBHelper.COLUMN_EVALUATION_ID,
            DBHelper.COLUMN_EVALUATION_SUBJECT,
            DBHelper.COLUMN_EVALUATION_WEIGHT,
            DBHelper.COLUMN_EVALUATION_MARK,
            DBHelper.COLUMN_EVALUATION_COURSE_ID
    };

    public EvaluationDAO(Context context){
        dbHelper = new DBHelper(context);
        this.context = context;

        try{
            open();
        } catch(SQLException e){
            Log.e(TAG, "SQLException on openning database " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void open() throws SQLException{
        database = dbHelper.getWritableDatabase();
    }

    public void close(){
        dbHelper.close();
    }
public ArrayList<Evaluation> getAllEvaluations(){
        ArrayList<Evaluation> listEvaluation = new ArrayList<Evaluation>();

        Cursor cursor = database.query(DBHelper.TABLE_EVALUATIONS, allColumns, null, null,
                null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()){
            Evaluation evaluation = cursorToEvaluation(cursor);
            listEvaluation.add(evaluation);
            cursor.moveToNext();
        }


        cursor.close();
        return listEvaluation; 

 }

HBHelper.java

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

    public static final String TAG = "DBHelper";

    private static final String DATABASE_NAME = "courses.db";
    private static final int DATABASE_VERSION = 1;

    //Columns of the course table
    public static final String TABLE_COURSES = "course";
    public static final String COLUMN_COURSE_ID = "_id";
    public static final String COLUMN_COURSE_SUBJECT = "COURSE_SUBJECT";
    public static final String COLUMN_COURSE_MARK = "mark";

    public static final String TABLE_EVALUATIONS = "evaluation";
    public static final String COLUMN_EVALUATION_ID = COLUMN_COURSE_ID;
    public static final String COLUMN_EVALUATION_SUBJECT = "evaluation_subject";
    public static final String COLUMN_EVALUATION_MARK = "mark";
    public static final String COLUMN_EVALUATION_WEIGHT = "weight";
    public static final String COLUMN_EVALUATION_COURSE_ID = "course_id";

    private static final String SQL_CREATE_TABLE_COURSES = "CREATE TABLE " + TABLE_COURSES + "("
            + COLUMN_COURSE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_COURSE_SUBJECT + " TEXT NOT NULL, "
            + COLUMN_COURSE_MARK + " TEXT NOT NULL " + ");";

    private static final String SQL_CREATE_TABLE_EVALUATION = "CREATE TABLE " + TABLE_EVALUATIONS + "("
            + COLUMN_EVALUATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_EVALUATION_SUBJECT + " TEXT NOT NULL, "
            + COLUMN_EVALUATION_MARK + " TEXT NOT NULL, "
            + COLUMN_EVALUATION_WEIGHT + " TEXT NOT NULL, "
            + COLUMN_EVALUATION_COURSE_ID + " INTEGER NOT NULL "
            +");";


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

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(SQL_CREATE_TABLE_COURSES);
        database.execSQL(SQL_CREATE_TABLE_EVALUATION);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG,
                "Upgrading the database from version " + oldVersion + " to "+ newVersion);
        // clear all data
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSES);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVALUATIONS);

        // recreate the tables
        onCreate(db);
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }
}

0 个答案:

没有答案