gpt4 book ai didi

Android: android.database.sqlite.SQLiteException: no such column: _id: 编译时:SELECT _id, category, summary, description, status FROM todo

转载 作者:行者123 更新时间:2023-11-29 17:40:41 24 4
gpt4 key购买 nike

我是安卓新手。我尝试使用 fragment 来实现待办事项列表应用程序。使用 listadpater 来显示 ListView 。但是,在我建立数据库后,使用另一个数据源文件来实现操作,其中一个是getAllTodos(),然后我就报错了:no column _id:然后我将代码复制到一个新项目中,使用Activity创建数据源的 ListView ,看起来很好,没有“不幸停止”。这是我的代码:第一个是 fragment 类中的 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)

最佳答案

您是否在运行应用程序后添加了列“_id”(即数据库已经创建,但没有列 _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");
}

关于Android: android.database.sqlite.SQLiteException: no such column: _id: 编译时:SELECT _id, category, summary, description, status FROM todo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28890777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com