2012-08-26 22 views
-1

我在爲我的程序創建和加載我的數據庫時遇到了錯誤。我得到一個錯誤「沒有這樣的列:負責:,在編譯: SELECT ID,名稱,負責,優先 FROM任務」Android:SQLiteDatabase列不存在錯誤

有人能指出我做了什麼錯。由於

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

您的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" + 
    ");" 
); 
+0

我的不好,我修復了這個問題,但仍然遇到同樣的問題。 –

+0

清除應用程序數據並重新啓動應用程序。這將導致DDL腳本再次運行,這將再次創建表。 – Gopinath

+0

僅當SQLite數據庫根本不存在時,纔會調用onCreate,而不是每次打開該應用程序。修復CREATE TABLE後,可能需要強制它重新創建數據庫。 –

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" + 
    ");" 
);