2

您好,我在数据库中创建了一个表。当我运行它时,我看到没有名为 endTime 的列的疯狂异常。问题是我看到我在 onCreate 方法上创建了该列。

编码:

package com.example.pref;

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.SQLiteOpenHelper;

public class Databasehandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

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

    // Contacts table name
    private static final String TABLE_SCHEDULE = "schedule";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_START_TIME = "StartTime";
    private static final String KEY_END_TIME = "EndTime";
    private static final String KEY_MONDAY = "Monday";
    private static final String KEY_TUESDAY = "Tuesday";
    private static final String KEY_WEDNESDAY = "Wednesday";
    private static final String KEY_THURSDAY = "Thursday";
    private static final String KEY_SATURDAY = "Saturday";
    private static final String KEY_FRIDAY = "Friday";
    private static final String KEY_SUNDAY = "Sunday";

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

    // + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_SCHEDULE_TABLE = "CREATE TABLE " + TABLE_SCHEDULE + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_START_TIME
                + " BIGINT," + KEY_END_TIME + " BIGINT," + KEY_MONDAY
                + " INTEGER," + KEY_TUESDAY + " INTEGER," + KEY_WEDNESDAY
                + " INTEGER," + KEY_THURSDAY + " INTEGER," + KEY_FRIDAY
                + " INTEGER," + KEY_SATURDAY + " INTEGER," + KEY_SUNDAY
                + " INTEGER" + ")";
        db.execSQL(CREATE_SCHEDULE_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCHEDULE);

        // Create tables again
        onCreate(db);
    }

    // Adding new Preference
    public void addPreference(Preference pref) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, pref.getID());
        values.put(KEY_START_TIME, pref.getStartTime());
        values.put(KEY_END_TIME, pref.getEndTime());
        values.put(KEY_MONDAY, pref.getMonday());
        values.put(KEY_TUESDAY, pref.getTuesday());
        values.put(KEY_WEDNESDAY, pref.getWednesday());
        values.put(KEY_THURSDAY, pref.getThursday());
        values.put(KEY_FRIDAY, pref.getFriday());
        values.put(KEY_SATURDAY, pref.getSaturday());
        values.put(KEY_SUNDAY, pref.getSunday());
        // Inserting Row
        db.insert(TABLE_SCHEDULE, null, values);
        db.close(); // Closing database connection
    }

    // Getting single message
    public Preference getPreference(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_SCHEDULE, new String[] { KEY_ID,
                KEY_START_TIME, KEY_END_TIME, KEY_MONDAY, KEY_TUESDAY,
                KEY_WEDNESDAY, KEY_THURSDAY, KEY_FRIDAY, KEY_SATURDAY,
                KEY_SUNDAY }, KEY_ID + "=?contact",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Preference pref = new Preference(cursor.getInt(0), cursor.getInt(1),
                cursor.getInt(2), cursor.getInt(3), cursor.getInt(4),
                cursor.getInt(5), cursor.getInt(6), cursor.getInt(7),
                cursor.getInt(8), cursor.getInt(9));
        // return contact
        return pref;
    }

    // Getting All messages
    public List<Preference> getAllPreferences() {
        List<Preference> preferenceList = new ArrayList<Preference>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SCHEDULE;

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Preference pref = new Preference();
                pref.setID(Integer.parseInt(cursor.getString(0)));
                pref.setStartTime(Integer.parseInt(cursor.getString(1)));
                pref.setEndTime(Integer.parseInt(cursor.getString(2)));
                pref.setMonday(Integer.parseInt(cursor.getString(3)));
                pref.setTuesday(Integer.parseInt(cursor.getString(4)));
                pref.setWednesday(Integer.parseInt(cursor.getString(5)));
                pref.setThursday(Integer.parseInt(cursor.getString(6)));
                pref.setFriday(Integer.parseInt(cursor.getString(7)));
                pref.setSaturday(Integer.parseInt(cursor.getString(8)));
                pref.setSunday(Integer.parseInt(cursor.getString(9)));

                // Adding contact to list
                preferenceList.add(pref);
            } while (cursor.moveToNext());
        }

        // return contact list
        return preferenceList;
    }

    // Getting All messages

    public List<Preference> getPreferences(int index, int limit) {
        List<Preference> preferenceList = new ArrayList<Preference>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SCHEDULE + " LIMIT "
                + index + ", " + limit;

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Preference pref = new Preference();
                pref.setID(Integer.parseInt(cursor.getString(0)));
                pref.setStartTime(Integer.parseInt(cursor.getString(1)));
                pref.setEndTime(Integer.parseInt(cursor.getString(2)));
                pref.setMonday(Integer.parseInt(cursor.getString(3)));
                pref.setTuesday(Integer.parseInt(cursor.getString(4)));
                pref.setWednesday(Integer.parseInt(cursor.getString(5)));
                pref.setThursday(Integer.parseInt(cursor.getString(6)));
                pref.setFriday(Integer.parseInt(cursor.getString(7)));
                pref.setSaturday(Integer.parseInt(cursor.getString(8)));
                pref.setSunday(Integer.parseInt(cursor.getString(9)));

                // Adding contact to list
                preferenceList.add(pref);
            } while (cursor.moveToNext());
        }

        // return contact list
        return preferenceList;
    }

    // Getting message Count
    public int getPreferenceCount() {
        // String countQuery = "SELECT * FROM " + TABLE_SCHEDULE;
        String countQuery = "SELECT count(id) FROM " + TABLE_SCHEDULE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        // return count
        return cursor.getCount();
    }

    // Updating single message
    public int updatePreference(Preference pref) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, pref.getID());
        values.put(KEY_START_TIME, pref.getStartTime());
        values.put(KEY_END_TIME, pref.getEndTime());
        values.put(KEY_MONDAY, pref.getMonday());
        values.put(KEY_TUESDAY, pref.getTuesday());
        values.put(KEY_WEDNESDAY, pref.getWednesday());
        values.put(KEY_THURSDAY, pref.getThursday());
        values.put(KEY_FRIDAY, pref.getFriday());
        values.put(KEY_SATURDAY, pref.getSaturday());
        values.put(KEY_SUNDAY, pref.getSunday());

        // updating row
        return db.update(TABLE_SCHEDULE, values, KEY_ID + " = ?",
                new String[] { String.valueOf(pref.getID()) });
    }

    // Deleting single message
    public void deleteMessage(Preference pref) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_SCHEDULE, KEY_ID + " = ?",
                new String[] { String.valueOf(pref.getID()) });

        // db.close();
    }

    public int deleteAllMessages() {
        SQLiteDatabase db = this.getWritableDatabase();

        return db.delete(TABLE_SCHEDULE, null, null);

    }

}

日志猫

08-10 11:24:29.976: E/Database(2997): Error inserting id=1 Saturday=0 Thursday=0 Monday=0 Tuesday=0 Wednesday=0 EndTime=0 StartTime=0 Friday=0 Sunday=0
08-10 11:24:29.976: E/Database(2997): android.database.sqlite.SQLiteException: table schedule has no column named EndTime: , while compiling: INSERT INTO schedule(id, Saturday, Thursday, Monday, Tuesday, Wednesday, EndTime, StartTime, Friday, Sunday) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

谢谢你。/mkounal

4

0 回答 0