SQLite数据库不能正确编译?

时间:2015-01-17 21:40:07

标签: java android sqlite

我正在创建一个监控出勤率的Android应用程序,但是当我尝试使用sqlite数据库中的用户详细信息登录时,我发现错误,表示列不存在。

DBHelper.class

package uk.ac.qub.qubattend;

import java.util.ArrayList;

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

public class DBHelper extends SQLiteOpenHelper {
    // Database Version
    private static final int DATABASE_VERSION = 6;
    // Database Name
    private static final String DATABASE_NAME = "QUBAttendanceDB";
    // tasks table name
    private static final String TABLE_StudentInfo = "studentinfo";
    // Table Columns names
    // id column
    private static final String KEY_ID = "StudentId";
    // question column
    private static final String KEY_PASS = "password";
    // answer column
    private static final String KEY_WEEK1 = "Week1";
    private static final String KEY_WEEK2 = "Week2";
    private static final String KEY_WEEK3 = "Week3";
    private static final String KEY_WEEK4 = "Week4";
    private static final String KEY_WEEK5 = "Week5";
    private static final String KEY_WEEK6 = "Week6";
    private static final String KEY_WEEK7 = "Week7";
    private static final String KEY_WEEK8 = "Week8";
    private static final String KEY_WEEK9 = "Week9";
    private static final String KEY_WEEK10 = "Week10";
    private static final String KEY_WEEK11 = "Week11";
    private static final String KEY_WEEK12 = "Week12";

    // Database constant
    private SQLiteDatabase dbase;

    /**
     * Method to check the db name and version
     * 
     * @param context
     */
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase = db;
        // Create Table method
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_StudentInfo + " ( "
                + KEY_ID + " TEXT," + KEY_PASS + " TEXT, " + KEY_WEEK1
                + "TEXT, " + KEY_WEEK2 + "TEXT, " + KEY_WEEK3 + "TEXT, "
                + KEY_WEEK4 + "TEXT, " + KEY_WEEK5 + "TEXT, " + KEY_WEEK6
                + "TEXT, " + KEY_WEEK7 + "TEXT, " + KEY_WEEK8 + "TEXT, "
                + KEY_WEEK9 + "TEXT, " + KEY_WEEK10 + "TEXT, " + KEY_WEEK11
                + "TEXT, " + KEY_WEEK12 + "TEXT" + ")";
        db.execSQL(sql);
        // implementing add questions method
        addAttendance();

    }

    /**
     * Add Questions Method using Question Object with question, 4 options and
     * correct answer
     */
    private void addAttendance() {
        Student s1 = new Student("40023798", "letmeinhere", "90", "80", "0",
                "0", "0", "0", "0", "0", "0", "0", "0", "0");
        this.addStudent(s1);

    }

    // Checking that existing database table called TABLE_QUEST is dropped
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_StudentInfo);
        // Create tables again
        onCreate(db);
    }

    /**
     * method for adding a new question into the database
     * 
     * @param quest
     */
    public void addStudent(Student student) {
        ContentValues values = new ContentValues();
        values.put(KEY_ID, student.getStudentId());
        values.put(KEY_PASS, student.getPassword());
        values.put(KEY_WEEK1, student.getWeek1());
        values.put(KEY_WEEK2, student.getWeek2());
        values.put(KEY_WEEK3, student.getWeek3());
        values.put(KEY_WEEK4, student.getWeek4());
        values.put(KEY_WEEK5, student.getWeek5());
        values.put(KEY_WEEK6, student.getWeek6());
        values.put(KEY_WEEK7, student.getWeek7());
        values.put(KEY_WEEK8, student.getWeek8());
        values.put(KEY_WEEK9, student.getWeek9());
        values.put(KEY_WEEK10, student.getWeek10());
        values.put(KEY_WEEK11, student.getWeek11());
        values.put(KEY_WEEK12, student.getWeek12());
        // Inserting Row with question

        dbase.insert(TABLE_StudentInfo, null, values);
    }

    /**
     * Method to move questions in database to an ArrayList
     * 
     * @return
     */
    public ArrayList<Student> getAllStudents() {
        ArrayList<Student> studentList = new ArrayList<Student>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_StudentInfo;
        dbase = this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Student student = new Student();
                student.setStudentId(cursor.getString(0));
                student.setPassword(cursor.getString(1));
                student.setWeek1(cursor.getString(2));
                student.setWeek2(cursor.getString(3));
                student.setWeek3(cursor.getString(4));
                student.setWeek4(cursor.getString(5));
                student.setWeek5(cursor.getString(6));
                student.setWeek6(cursor.getString(7));
                student.setWeek7(cursor.getString(8));
                student.setWeek8(cursor.getString(9));
                student.setWeek9(cursor.getString(10));
                student.setWeek10(cursor.getString(11));
                student.setWeek11(cursor.getString(12));
                student.setWeek12(cursor.getString(13));
                studentList.add(student);
            } while (cursor.moveToNext());
        }
        // return question list
        return studentList;
    }

    /**
     * Method to Check Database number of rows
     * 
     * @return
     */
    public int rowcount() {
        int row = 0;
        // select all query
        String selectQuery = "SELECT  * FROM " + TABLE_StudentInfo;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        row = cursor.getCount();
        return row;
    }
}

下面是显示错误的日志,但我不知道为什么会抛出错误:

01-17 16:26:51.616: E/SQLiteLog(1667): (1) table studentinfo has no column named Week8
01-17 16:26:51.656: E/SQLiteDatabase(1667): Error inserting StudentId=40023798 Week8=0 Week9=0 Week4=0 Week5=0 password=letmeinhere Week6=0 Week7=0 Week3=0 Week2=80 Week12=0 Week1=90 Week11=0 Week10=0
01-17 16:26:51.656: E/SQLiteDatabase(1667): android.database.sqlite.SQLiteException: table studentinfo has no column named Week8 (code 1): , while compiling: INSERT INTO studentinfo(StudentId,Week8,Week9,Week4,Week5,password,Week6,Week7,Week3,Week2,Week12,Week1,Week11,Week10) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at uk.ac.qub.qubattend.DBHelper.addStudent(DBHelper.java:109)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at uk.ac.qub.qubattend.DBHelper.addAttendance(DBHelper.java:73)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at uk.ac.qub.qubattend.DBHelper.onCreate(DBHelper.java:62)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at uk.ac.qub.qubattend.DBHelper.onUpgrade(DBHelper.java:83)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
01-17 16:26:51.656: E/SQLiteDatabase(1667):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)

如果有人知道为什么它没有拿到第8列,我会非常感激!

1 个答案:

答案 0 :(得分:0)

所有KEY_WEEK...值都附加"TEXT, ",而不是" TEXT, ",因此您的列和数据类型名称应该一起运行。

使用StringBuilder(更快)或String.format()(更易于阅读),而不是使用字符串连接,您可能会得到更好的服务。