android.database.sqlite.SQLiteException:没有这样的列:due_date(代码1):

时间:2014-05-01 09:58:54

标签: android sqlite

我正在使用eclipse进行android开发。在我的应用程序中,我将有一个查询来获取存储在数据库中的赋值。我希望它在截止日期前订购,但是当我尝试在模拟器上运行我的应用程序并且模拟器显示如下错误时遇到了这个问题:

“不幸的是,分配经理#2已停止。”

P / s:我已经创建了“due_date”列,但似乎数据库没有在我的数据库中创建该列。

logcat的:

05-01 05:54:54.783: D/dalvikvm(2152): GC_FOR_ALLOC freed 48K, 5% free 2947K/3076K,   paused 35ms, total 37ms
05-01 05:54:54.783: I/dalvikvm-heap(2152): Grow heap (frag case) to 3.557MB for 635812-byte allocation
05-01 05:54:54.823: D/dalvikvm(2152): GC_FOR_ALLOC freed 2K, 4% free 3566K/3700K, paused 31ms, total 31ms
05-01 05:54:54.963: E/SQLiteLog(2152): (1) no such column: due_date
05-01 05:54:54.973: D/AndroidRuntime(2152): Shutting down VM
05-01 05:54:54.973: W/dalvikvm(2152): threadid=1: thread exiting with uncaught exception (group=0xb3b0cba8)
05-01 05:54:54.983: E/AndroidRuntime(2152): FATAL EXCEPTION: main
05-01 05:54:54.983: E/AndroidRuntime(2152): Process: com.mada.assignmentmanager, PID: 2152
05-01 05:54:54.983: E/AndroidRuntime(2152): java.lang.RuntimeException: Unable to resume activity {com.mada.assignmentmanager/com.mada.assignmentmanager.AssignmentMainActivity}: android.database.sqlite.SQLiteException: no such column: due_date (code 1): , while compiling: SELECT _id, due_date FROM AssignmentTable ORDER BY due_date
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.os.Looper.loop(Looper.java:136)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at java.lang.reflect.Method.invoke(Method.java:515)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at dalvik.system.NativeStart.main(Native Method)
05-01 05:54:54.983: E/AndroidRuntime(2152): Caused by: android.database.sqlite.SQLiteException: no such column: due_date (code 1): , while compiling: SELECT _id, due_date FROM AssignmentTable ORDER BY due_date
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at com.mada.assignmentmanager.AssignmentManagerDatabase.getAllAssignments(AssignmentManagerDatabase.java:88)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at com.mada.assignmentmanager.AssignmentMainActivity.onResume(AssignmentMainActivity.java:42)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.Activity.performResume(Activity.java:5310)
05-01 05:54:54.983: E/AndroidRuntime(2152):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
05-01 05:54:54.983: E/AndroidRuntime(2152):     ... 12 more
05-01 05:54:57.163: I/Process(2152): Sending signal. PID: 2152 SIG: 9

数据库代码:

package com.mada.assignmentmanager;

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

public class AssignmentManagerDatabase {

public static final String KEY_ROWID = "_id";
public static final String KEY_MODULECODE = "module_code";
public static final String KEY_ASSIGNMENTNAME = "assignment_name";
public static final String KEY_MARKSPROPORTION = "marks_proportion";
public static final String KEY_DUEDATE = "due_date";
public static final String KEY_PROGRESS = "_progress";

private static final String DATABASE_NAME = "Assignmentdb";
private static final String DATABASE_TABLE = "AssignmentTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
String cmd;

private static class DbHelper extends SQLiteOpenHelper {

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

    private static final String CREATE_NEW_DATABASE = "CREATE TABLE "
            + DATABASE_TABLE + " (" + KEY_ROWID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_MODULECODE
            + " TEXT  NOT NULL, " + KEY_ASSIGNMENTNAME + " TEXT NOT NULL, "
            + KEY_MARKSPROPORTION + " INTEGER, " + KEY_DUEDATE
            + " TEXT NOT NULL, " + KEY_PROGRESS + " INTERGER);";

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("AssignmentDBAdapter", "Upgrading from version" + oldVersion
                + " to " + newVersion
                + ", which will destroy all the old data.");
        // Drop the old table.
        db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
        // Create a new one.
        onCreate(db);
    }
}

public AssignmentManagerDatabase(Context c) {
    ourContext = c;
}

public AssignmentManagerDatabase open() {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public void createEntry(String moduleCode, String assignmentName,
        String marksProportion, String dueDate, String assignmentProgress) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_MODULECODE, moduleCode);
    cv.put(KEY_ASSIGNMENTNAME, assignmentName);
    cv.put(KEY_MARKSPROPORTION, marksProportion);
    cv.put(KEY_DUEDATE, dueDate);
    cv.put(KEY_PROGRESS, assignmentProgress);
    ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public Cursor getAllAssignments() {
    // String[] columns = new String[]{KEY_ROWID, KEY_MODULECODE,
    // KEY_ASSIGNMENTNAME, KEY_MARKSPROPORTION, KEY_DUEDATE, KEY_PROGRESS};
    String[] columns = new String[] { KEY_ROWID, KEY_DUEDATE };
    return ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
            null, KEY_DUEDATE);
}

public void deleteAssignment(long id) {
    ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + id, null);
}

public Cursor getOneAssignment(long assignID) {
    // TODO Auto-generated method stub
    return ourDatabase.query(DATABASE_TABLE, null, KEY_ROWID + "="
            + assignID, null, null, null, null);
}

}

是因为我在onUpgrade()部分做错了吗?

1 个答案:

答案 0 :(得分:5)

由于此错误,未创建表:

KEY_PROGRESS + " INTERGER);";

删除第一个'R'。它应该是:

KEY_PROGRESS + " INTEGER);";