-1

我在为我的程序创建和加载数据库时遇到了错误。我收到“没有这样的列:负责:,编译时:SELECT id, name, responsibility, priority FROM tasks”的错误

有人可以指出我做错了什么。谢谢

public class TasksSQLiteOpenHelper extends SQLiteOpenHelper {

public static final int VERSION = 1;
public static final String DB_NAME  = "tasks_db.sqlite";
public static final String TASKS_TABLE  = "tasks";
public static final String TASK_ID = "id";
public static final String TASK_NAME = "name";
public static final String TASK_COMPLETE  = "complete";
public static final String TASK_RESPONSIBLE  = "responsible";
public static final String TASK_PRIORITY  = "priority";

public TasksSQLiteOpenHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    dropAndCreate(db);
}

protected void dropAndCreate(SQLiteDatabase db) {
    db.execSQL("drop table if exists " + TASKS_TABLE + ";");
    createTables(db);
}

protected void createTables(SQLiteDatabase db) {
    db.execSQL(
            "create table " + TASKS_TABLE +" (" +
            TASK_ID + " integer primary key autoincrement not null," +
            TASK_NAME + " text," +
            TASK_COMPLETE + " text," +
            TASK_RESPONSIBLE + " text" +
            TASK_PRIORITY + " integer" +
            ");"
        );
}
}

-

public class TaskManagerApplication extends Application {

private SQLiteDatabase database;
private ArrayList<Task> currentTasks;

@Override
public void onCreate() {
    super.onCreate();
    TasksSQLiteOpenHelper helper = new TasksSQLiteOpenHelper(this);
    database = helper.getWritableDatabase();
    if (null == currentTasks) {
        loadTasks();
    }
}

private void loadTasks() {
    currentTasks = new ArrayList<Task>();
    Cursor tasksCursor = database.query(TASKS_TABLE, new String[] {
            TASK_ID, 
            TASK_NAME,
            TASK_RESPONSIBLE,
            TASK_PRIORITY,
            TASK_COMPLETE}, null, null, null, null, String.format("%s,%s", TASK_COMPLETE, TASK_NAME));
    tasksCursor.moveToFirst();
    Task t;
    if (! tasksCursor.isAfterLast()) {
        do {
            int id = tasksCursor.getInt(0);
            String name  = tasksCursor.getString(1);
            String priority  = tasksCursor.getString(2);
            String responsible = tasksCursor.getString(3);
            String boolValue = tasksCursor.getString(4);
            boolean complete = Boolean.parseBoolean(boolValue);
            t = new Task(name, priority, responsible);
            t.setId(id);
            t.setComplete(complete);
            currentTasks.add(t);
        } while (tasksCursor.moveToNext());
    }

    tasksCursor.close();
}
}
4

2 回答 2

2

您的CREATE TABLE;中有错字

db.execSQL(
    "create table " + TASKS_TABLE +" (" +
    TASK_ID + " integer primary key autoincrement not null," +
    TASK_NAME + " text," +
    TASK_COMPLETE + " text," +
    TASK_RESPONSIBLE + " text" + // <--- missing a comma
    TASK_PRIORITY + " integer" +
    ");"
);
于 2012-08-26T16:36:42.643 回答
0

您的代码中有一个小的输入错误。更正版本:

db.execSQL(
    "create table " + TASKS_TABLE +" (" +
    TASK_ID + " integer primary key autoincrement not null," +
    TASK_NAME + " text," +
    TASK_COMPLETE + " text," +
    TASK_RESPONSIBLE + " text," + 
    TASK_PRIORITY + " integer" +
   ");"
 );
于 2012-08-26T16:39:56.770 回答