我为数据库表的名称创建了这个类:
public class TableDatas {
public TableDatas() {
}
public static abstract class TableInfo implements BaseColumns//columns of tables
{
public static final String first_Name = "first_name" ;
public static final String last_Name = "last_name" ;
public static final String sex = "sex" ;
public static final String birth_date = "birth_date" ;
public static final String db_name = "UserInfo" ;
public static final String tbl_name = "UserData" ;
}
}
在db helper类中我创建了一个游标函数:
public Cursor getInfo(DatabaseOperat dop) {
SQLiteDatabase sq = dop.getReadableDatabase();
String columns[] = {TableInfo.first_Name, TableInfo.last_Name, TableInfo.sex, TableInfo.birth_date};
Cursor CR = sq.query(TableInfo.tbl_name, columns, null, null, null, null, null);
return CR;
}
在mainActivity中,我这样做是为了保存数组列表中列的数据:
DatabaseOperat DOP = new DatabaseOperat(CTX);
Cursor CR = DOP.getInfo(DOP);
DOP.getWritableDatabase();
CR.moveToFirst();
while (!CR.isAfterLast()) {
Fnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.first_Name)));
Lnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.last_Name)));
Birthdates.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.sex)));
Sex.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.birth_date)));
// Adding contact to list
}
CR.close();
Toast.makeText(getApplicationContext(), "Cursor Success", Toast.LENGTH_LONG).show();
获取中的错误如下:
android.database.sqlite.SQLiteException:...没有这样的列:first_name(代码1):,同时编译:SELECT first_name,last_name,sex,birth_date FROM UserData 任何错误在哪里?
答案 0 :(得分:0)
更改此代码:
DatabaseOperat DOP = new DatabaseOperat(CTX);
Cursor CR = DOP.getInfo(DOP);
DOP.getWritableDatabase();
CR.moveToFirst();
while (!CR.isAfterLast()) {
Fnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.first_Name)));
Lnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.last_Name)));
Birthdates.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.sex)));
Sex.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.birth_date)));
// Adding contact to list
}
CR.close();
Toast.makeText(getApplicationContext(), "Cursor Success", Toast.LENGTH_LONG).show();
致:
DatabaseOperat DOP = new DatabaseOperat(CTX);
DOP.getWritableDatabase();
Cursor CR = DOP.getInfo(DOP);
CR.moveToFirst();
while (!CR.isAfterLast()) {
Fnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.first_Name)));
Lnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.last_Name)));
Birthdates.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.sex)));
Sex.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.birth_date)));
// Adding contact to list
}
CR.close();
Toast.makeText(getApplicationContext(), "Cursor Success", Toast.LENGTH_LONG).show();
首先调用getWritableDatabase()而不是getInfo()