Android:android.database.sqlite.SQLiteException:没有这样的列:_id :,同时编译:SELECT _id,category,summary,description,status FROM todo

时间:2015-03-06 01:11:20

标签: android sqlite android-fragments

我是android的新手。我尝试使用片段来实现待办事项列表应用程序。使用listadpater显示列表视图。但是,在我构建数据库并使用另一个数据源文件来实现操作之后,其中一个是getAllTodos(),然后我遇到了错误:no column _id: 然后我将代码复制到一个新项目,使用Activity创建数据源的列表视图,看起来很好,没有"不幸停止"。这是我的代码:首先是片段类中的onCreate,第二个是数据源函数,第三个是我的databaseHelper,最后是错误。

这些onCreate函数似乎有很大问题。

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_todo, container, false);
        listTask=(ListView) rootView.findViewById(android.R.id.list);


        return rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Activity activity = getActivity();

        if (activity != null) {
            // Create an instance of the custom adapter for the GridView. A static array of location data
            // is stored in the Application sub-class for this app. This data would normally come
            // from a database or a web service.
            datasource = new TodoDataSource(getActivity());
            datasource.open();

            todoList = datasource.getAllTodos();
            adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
            //ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
            listTask.setAdapter(adapt); 
        }
    }

这是数据源getAllTodos()和allColumns值

      private String[] allColumns = { 
                DatabaseHelper.COLUMN_ID, 
                DatabaseHelper.COLUMN_CATEGORY, 
                DatabaseHelper.COLUMN_SUMMARY, 
                DatabaseHelper.COLUMN_DESCRIPTION, 
                DatabaseHelper.COLUMN_STATUS };

            public List<Todo> getAllTodos() {
            List<Todo> todos = new ArrayList<Todo>();



            Cursor cursor = database.query(DatabaseHelper.TABLE_TODO, allColumns, null, null, null, null, null); // here came with the error

            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                Todo todo = cursorToTodo(cursor);
                todos.add(todo);
                cursor.moveToNext();
            }
            // Make sure to close the cursor
            cursor.close();
            return todos;
        }

public class DatabaseHelper extends SQLiteOpenHelper {

    // Database table
          public static final String TABLE_TODO = "todo";
          public static final String COLUMN_ID = "_id";
          public static final String COLUMN_CATEGORY = "category";
          public static final String COLUMN_SUMMARY = "summary";
          public static final String COLUMN_DESCRIPTION = "description";
          public static final String COLUMN_STATUS = "status";

      private static final String DATABASE_NAME = "todolist.db";
      private static final int DATABASE_VERSION = 1;

    // Database creation SQL statement
          private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS " 
              + TABLE_TODO
              + " (" 
              + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
              + COLUMN_CATEGORY + " TEXT NOT NULL, " 
              + COLUMN_SUMMARY + " TEXT NOT NULL, " 
              + COLUMN_DESCRIPTION + " TEXT, " 
              + COLUMN_STATUS + " INTEGER NOT NULL);";


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

      // Method is called during creation of the database
      @Override
      public void onCreate(SQLiteDatabase database) {
          database.execSQL(DATABASE_CREATE);
      }

      // Method is called during an upgrade of the database,
      // e.g. if you increase the database version
      @Override
      public void onUpgrade(SQLiteDatabase database, int oldVersion,
          int newVersion) {
        //TodoTable.onUpgrade(database, oldVersion, newVersion);
          Log.w(DatabaseHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                            + newVersion + ", which will destroy all old data");
            database.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
            onCreate(database);
      }

}

错误:

03-06 01:02:41.318: E/AndroidRuntime(9091): FATAL EXCEPTION: main
03-06 01:02:41.318: E/AndroidRuntime(9091): android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, category, summary, description, status FROM todo
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at kidslist.sqlite.helper.TodoDataSource.getAllTodos(TodoDataSource.java:93)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at com.example.kidslist.TodoFragment.onActivityCreated(TodoFragment.java:68)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:847)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.app.BackStackRecord.run(BackStackRecord.java:622)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.os.Handler.handleCallback(Handler.java:605)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.os.Looper.loop(Looper.java:137)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at java.lang.reflect.Method.invoke(Method.java:511)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
03-06 01:02:41.318: E/AndroidRuntime(9091):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您是否添加了列&#39; _id&#39;运行应用程序后(即数据库已经创建没有列_id)?

最简单的方法是清除设置中的应用程序数据(如果您的应用尚未启动)以删除数据库。

如果您的应用已经推出,可以尝试按照

private static final int DATABASE_VERSION = 2; // indicate database update

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2)
        db.execSQL("ALTER TABLE "+ TABLE_TODO +" ADD "+ COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT");
}

答案 1 :(得分:0)

在android的sqlite中,有一个_id列,当你将这个数据库用于adpters 时,不能省略。由于您尚未发布APP,因此可以考虑将此列添加到数据库中。
您可以参考此问题进行进一步研究。
About "_id" field in Android SQLite