SQLite 游标返回 IllegalStateException?
SQLite cursor returning IllegalStateException?
我正在尝试将元素添加到数据库,然后搜索特定条目。但是我得到一个游标错误。这是我的数据库代码 class。数据库只包含一个列。提前致谢。
public class DataB extends SQLiteOpenHelper {
private static final String db_name = "testing.db";
private static final int version = 1;
private static final String table_name = "students";
private static final String col_name="FirstName";
public DataB(Context context) {
super(context, db_name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String q = "CREATE TABLE " + table_name + " (FirstName TEXT PRIMARY KEY) ";
db.execSQL(q);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+table_name);
onCreate(db);
}
public int addData(String name)
{
ContentValues cv = new ContentValues();
cv.put(col_name, name);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(table_name, null, cv);
db.close();
return 1;
}
public String search(String string)
{
String dbstring=" ";
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM "+ table_name + " WHERE FirstName = '" + string+"'";
Cursor c = db.rawQuery(query,null);
c.moveToFirst();
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("FirstName"))!=null)
{
dbstring = c.getString(c.getColumnIndex("FirstName"));
}
}
return dbstring;
}
}
错误是
Caused by: java.lang.IllegalStateException: Couldn\'t read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
while 根本不工作,你永远不会改变光标位置,所以它总是正确的,所以制作一个 Whosebug
执行查询后,您应该询问光标是否不为空以及是否可以移动到第一个位置,如下所示:
if(c != null && c.moveToFirst()){
dbstring = c.getString(c.getColumnIndex(col_name));
}
或者在您的情况下,因为您拥有的唯一列是 FirstName,您可以这样看:
if(c != null && c.moveToFirst()){
dbstring = c.getString(0);
}
请记住,如果您更改了数据库中的任何内容,您应该升级数据库版本。
我正在尝试将元素添加到数据库,然后搜索特定条目。但是我得到一个游标错误。这是我的数据库代码 class。数据库只包含一个列。提前致谢。
public class DataB extends SQLiteOpenHelper {
private static final String db_name = "testing.db";
private static final int version = 1;
private static final String table_name = "students";
private static final String col_name="FirstName";
public DataB(Context context) {
super(context, db_name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String q = "CREATE TABLE " + table_name + " (FirstName TEXT PRIMARY KEY) ";
db.execSQL(q);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+table_name);
onCreate(db);
}
public int addData(String name)
{
ContentValues cv = new ContentValues();
cv.put(col_name, name);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(table_name, null, cv);
db.close();
return 1;
}
public String search(String string)
{
String dbstring=" ";
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM "+ table_name + " WHERE FirstName = '" + string+"'";
Cursor c = db.rawQuery(query,null);
c.moveToFirst();
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("FirstName"))!=null)
{
dbstring = c.getString(c.getColumnIndex("FirstName"));
}
}
return dbstring;
}
}
错误是
Caused by: java.lang.IllegalStateException: Couldn\'t read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
while 根本不工作,你永远不会改变光标位置,所以它总是正确的,所以制作一个 Whosebug
执行查询后,您应该询问光标是否不为空以及是否可以移动到第一个位置,如下所示:
if(c != null && c.moveToFirst()){
dbstring = c.getString(c.getColumnIndex(col_name));
}
或者在您的情况下,因为您拥有的唯一列是 FirstName,您可以这样看:
if(c != null && c.moveToFirst()){
dbstring = c.getString(0);
}
请记住,如果您更改了数据库中的任何内容,您应该升级数据库版本。