在添加/删除单行之前,SQLite数据库工作正常,直到我添加了 getAllRecords方法并让它在Oncreate中运行。
我花了很多时间修理它,但没有运气。
有人可以告诉我我做错了吗?
MySQLite_Database:
public static final String KEY_ID = "ROW_ID"; // (0)
public static final String KEY_ADD_REMOVE = "AddorRemove"; // (1)
public static final String GROUP_POS = "Group_Position"; // (2)
public static final String CHILD_POS = "Child_Position"; // (3)
public static final String DATABASE_TABLE = "Group_Child_Pos_Table";
public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_ADD_REMOVE, GROUP_POS, CHILD_POS};
private static final String POSITION_CREATE_SQL =
"CREATE TABLE " + getDatabaseTable() + "("+
KEY_ID + " INTEGER PRIMARY KEY," +
KEY_ADD_REMOVE + " TEXT," +
GROUP_POS+ " TEXT," +
CHILD_POS + " TEXT)";
private SQLiteDatabase db;
SQLiteDatabase getWritableDatabase = this.getWritableDatabase();
public MySQLite_Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CHILD_COUNT_CREATE);
db.execSQL(POSITION_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
// db.execSQL("DROP TABLE IF EXISTS " + CHILD_COUNT_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + POSITION_CREATE_SQL);
// create new tables
onCreate(db);
}
MainActivity:
private MySQLite_Database MySQLITE_DATABASE;
private SQLiteDatabase SQLite_database;
private Data_Holder_Class DATA_HOLDER_CLASS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list_view);
//Initialize variables here
MySQLITE_DATABASE = new MySQLite_Database(this);
try {
Open_SQLITE_DATABASE();
getAllRecords();
}
catch (Exception e)
{
Log.e("ERROR", "ERROROR");
}
public void getAllRecords() {
Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
if(cursor != null)
{
while(cursor.moveToNext()){
String GROUP_POSITION = cursor.getString(cursor.getColumnIndex("GROUP_POS"));
String CHILD_POSITION = cursor.getString(cursor.getColumnIndex("CHILD_POS"));
}
}
}
非常感谢
编辑:发布了工作代码..
我终于找到了我做错的事情......
这是完整的工作代码。这可能在将来帮助其他人。
BAD CODE:
public void getAllRecords() {
Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
if(cursor != null)
{
while(cursor.moveToNext()){
String GROUP_POSITION = cursor.getString(cursor.getColumnIndex("GROUP_POS"));
String CHILD_POSITION = cursor.getString(cursor.getColumnIndex("CHILD_POS"));
}
}
}
到
好的代码:
在MySQLite_Database中:
我创建了这个方法:
public SQLiteDatabase getWritableData() {
SQLiteDatabase getWritableDatabase = this.getWritableDatabase();
return getWritableDatabase;
}
在MainActivity中:
MySQLITE_DATABASE = new MySQLite_Database(this);
....
....
public void getAllRecords() {
Cursor cursor = MySQLITE_DATABASE.getWritableData().query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
if(cursor != null)
{
while(cursor.moveToNext()){
String GROUP_POSITION = cursor.getString(cursor.getColumnIndex(MySQLite_Database.GROUP_POS));
String CHILD_POSITION = cursor.getString(cursor.getColumnIndex(MySQLite_Database.CHILD_POS));
}
}
}
**基本上,在BAD CODE中,我有
private SQLiteDatabase SQLite_database
并将其用于错误的游标。 **光标实际上需要这个
SQLiteDatabase getWritableDatabase = this.getWritableDatabase();
}
感谢大家的帮助,尤其是mmlooloo。 mmlooloo真的帮助我抓住了另一个错误。我希望我的错误可以帮助别人。
答案 0 :(得分:1)
更改这些行:
String GROUP_POSITION = cursor.getString(cursor.getColumnIndex("GROUP_POS"));
String CHILD_POSITION = cursor.getString(cursor.getColumnIndex("CHILD_POS"));
到
String GROUP_POSITION = cursor.getString(cursor.getColumnIndex(GROUP_POS));
String CHILD_POSITION = cursor.getString(cursor.getColumnIndex(CHILD_POS));
和
Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
到
Cursor cursor = MySQLite_Database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
答案 1 :(得分:-2)
我认为你没有在那行中指定你想要的列
Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE,null,null,null,null,null,null);
功能查询需要:
String table,String [] columns,String selection,String [] selectionArgs,String groupBy,String having,String orderBy
并在列中放置null,因此您必须指定所需的列