安卓:SQLiteDatabase列不存在错误 [英] Android: SQLiteDatabase column doesn't exist error

查看:187
本文介绍了安卓:SQLiteDatabase列不存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个错误与创建和加载我的数据库为我的计划。我没有得到任何这样的列的错误:负责:,在编译:
SELECT ID,名称,负责,优先
FROM任务

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

 公共类TasksSQLiteOpenHelper扩展SQLiteOpenHelper {公共静态最终诠释VERSION = 1;
公共静态最后弦乐DB_NAME =tasks_db.sqlite;
公共静态最后弦乐TASKS_TABLE =任务;
公共静态最后弦乐TASK_ID =ID;
公共静态最后弦乐TASK_NAME =名;
公共静态最后弦乐TASK_COMPLETE =完成;
公共静态最后弦乐TASK_RESPONSIBLE =负责任;
公共静态最后弦乐TASK_PRIORITY =优先级;公共TasksSQLiteOpenHelper(上下文的背景下){
    超(背景下,DB_NAME,空,VERSION);
}@覆盖
公共无效的onCreate(SQLiteDatabase DB){
    dropAndCreate(DB);
}保护无效dropAndCreate(SQLiteDatabase DB){
    db.execSQL(+ TASKS_TABLE +是否存在删除表;);
    createTables(DB);
}保护无效createTables(SQLiteDatabase DB){
    db.execSQL(
            CREATE TABLE+ TASKS_TABLE +(+
            TASK_ID +整数主键自动增量不为空,+
            TASK_NAME +文字,+
            TASK_COMPLETE +文字,+
            TASK_RESPONSIBLE +文本+
            TASK_PRIORITY +整数+
            );
        );
}
}

-

 公共类TaskManagerApplication扩展应用{私人SQLiteDatabase数据库;
私人的ArrayList<任务> currentTasks;@覆盖
公共无效的onCreate(){
    super.onCreate();
    TasksSQLiteOpenHelper帮手=新TasksSQLiteOpenHelper(本);
    数据库= helper.getWritableDatabase();
    如果(空== currentTasks){
        loadTasks();
    }
}私人无效loadTasks(){
    currentTasks =新的ArrayList<任务>();
    光标tasksCursor = database.query(TASKS_TABLE,新的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();
    任务T;
    如果(!tasksCursor.isAfterLast()){
        做{
            INT ID = tasksCursor.getInt(0);
            字符串名称= tasksCursor.getString(1);
            串优先= tasksCursor.getString(2);
            串负责= tasksCursor.getString(3);
            串boolValue = tasksCursor.getString(4);
            布尔完成= Boolean.parseBoolean(boolValue);
            T =新的任务(名称,优先级负责);
            t.se​​tId(ID);
            t.se​​tComplete(完成);
            currentTasks.add(T);
        }而(tasksCursor.moveToNext());
    }    tasksCursor.close();
}
}


解决方案

您在你的 CREATE TABLE一个错字;

  db.execSQL(
    CREATE TABLE+ TASKS_TABLE +(+
    TASK_ID +整数主键自动增量不为空,+
    TASK_NAME +文字,+
    TASK_COMPLETE +文字,+
    TASK_RESPONSIBLE +文本+ //< ---缺少一个逗号
    TASK_PRIORITY +整数+
    );
);

I've encountered an error with creating and loading my database for my program. I get an error of "no such column: responsible: , while compiling: SELECT id, name, responsible, priority FROM tasks "

Can someone point out what I've done wrong. Thanks

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

解决方案

You have a typo in your 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" +
    ");"
);

这篇关于安卓:SQLiteDatabase列不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆