Android:SQLiteDatabase列不存在错误

时间:2012-08-26 16:32:57

标签: java android database sqlite

我在为我的程序创建和加载数据库时遇到错误。我得到一个错误“没有这样的专栏:负责人:,同时编译: SELECT id,name,responsible,priority 从任务“

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

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();
}
}

2 个答案:

答案 0 :(得分: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" +
    ");"
);

答案 1 :(得分: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" +
   ");"
 );