Hy guys,
我有关于SQL查询的错误。我找不到错误,我认为它来自于我创建表格的那一刻。如果你能帮助我,那就太好了。
public class dbVariablesCreate {
public static final String DB_NAME = "com.todolist.vladmac.mytodolist.db";
public static final int DB_VERSION = 1;
public class TaskEntry implements BaseColumns {
public static final String TABLE = "table_task";
public static final String COL_TASK = "task";
public static final String COL_COLOR = "colo";
}
}
public class dbRequery extends SQLiteOpenHelper {
public dbRequery(Context context) {
super(context, dbVariablesCreate.DB_NAME, null, dbVariablesCreate.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
/* String createTable = "CREATE TABLE " + dbVariablesCreate.TaskEntry.TABLE + " ( " +
dbVariablesCreate.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
dbVariablesCreate.TaskEntry.COL_TASK+" TEXT, "+dbVariablesCreate.TaskEntry.COL_COLOR+ " TEXT);";*/
db.execSQL("CREATE TABLE " + dbVariablesCreate.TaskEntry.TABLE + " ( " + dbVariablesCreate.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + dbVariablesCreate.TaskEntry.COL_TASK + " TEXT, " + dbVariablesCreate.TaskEntry.COL_COLOR + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + dbVariablesCreate.TaskEntry.TABLE);
onCreate(db);
}
}
重新查询
Cursor cursor = db.rawQuery("SELECT " + dbVariablesCreate.TaskEntry._ID + ", " + dbVariablesCreate.TaskEntry.COL_TASK ", " + dbVariablesCreate.TaskEntry.COL_COLOR + " FROM " + dbVariablesCreate.TaskEntry.TABLE, null);
感谢您的帮助。
答案 0 :(得分:0)
看起来你错过了添加这个
public static final String _ID = "_id";
你的代码? TaskEntry
public class dbVariablesCreate {
public static final String DB_NAME = "com.todolist.vladmac.mytodolist.db";
public static final int DB_VERSION = 1;
public class TaskEntry implements BaseColumns {
public static final String TABLE = "table_task";
public static final String _ID = "_id";
public static final String COL_TASK = "task";
public static final String COL_COLOR = "colo";
}
此处从查询中删除突出显示的;
db.execSQL(“CREATE TABLE”+ dbVariablesCreate.TaskEntry.TABLE +“(”+ dbVariablesCreate.TaskEntry._ID +“INTEGER PRIMARY KEY AUTOINCREMENT,”+ dbVariablesCreate.TaskEntry.COL_TASK +“TEXT,”+ dbVariablesCreate.TaskEntry .COL_COLOR +“TEXT);
”);
db.execSQL("CREATE TABLE " + dbVariablesCreate.TaskEntry.TABLE + " ( " + dbVariablesCreate.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + dbVariablesCreate.TaskEntry.COL_TASK + " TEXT, " + dbVariablesCreate.TaskEntry.COL_COLOR + " TEXT)");
答案 1 :(得分:0)
尝试使用此重新查询
public Cursor queryAll(){
//get a READABLE database instance:
SQLiteDatabase db = dbRequery.getReadableDatabase();
//query all:
//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
Cursor cursor = db.query(
dbVariablesCreate.TaskEntry.TABLE,null,
null,
null,
null,
null,
null );
return cursor ;
}