我正在尝试在android上使用sqlite数据库.Below是我用来访问sqlite数据库的SQL帮助程序类。
public class SQLHelper extends SQLiteOpenHelper{
static final String TABLE_NAME = "itemDetail";
static final String ID ="id INTEGER PRIMARY KEY AUTOINCREMENT";
static final String ITEM = "item";
static final String QUANTITY = "quantity";
static final String CREATE_TABLE = "CREATE TABLE "+SQLHelper.TABLE_NAME+"( " + ID +" , "+SQLHelper.ITEM+" TEXT "+" , "+SQLHelper.QUANTITY+" INTEGER"+" )";
static final String DB_NAME = "mydb";
public SQLHelper(Context context, int version) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
arg0.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
// create fresh books table
this.onCreate(arg0);
}
public long add(String item , String q){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ITEM, item);
values.put(QUANTITY , q);
long newr = db.insert(TABLE_NAME, "null", values);
db.close();
return newr;
}
public Cursor read(){
Cursor cc;
SQLiteDatabase db = this.getWritableDatabase();
Log.d("report","ok");
cc=db.rawQuery("SELECT * FROM itemDetail", null);
Log.d("report","ok2");
db.close();
//cc.moveToFirst();
Log.d("good",cc.getString(1));
return cc;
}
}
当我使用cc(Cursor对象)访问查询命令返回的行时,我的程序停止。我认为rawQuery没有返回任何行。请帮助。谢谢
logcat的
01-13 22:12:37.921: E/AndroidRuntime(341): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 23
01-13 22:12:37.921: E/AndroidRuntime(341): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-13 22:12:37.921: E/AndroidRuntime(341): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
01-13 22:12:37.921: E/AndroidRuntime(341): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-13 22:12:37.921: E/AndroidRuntime(341): at it.is.appone.SQLHelper.read(SQLHelper.java:63)
01-13 22:12:37.921: E/AndroidRuntime(341): at it.is.appone.Report.show(Report.java:36)
01-13 22:12:37.921: E/AndroidRuntime(341): ... 14 more
下面是我试图访问数据库的活动类中的函数。
public void show(View view){
//Toast.makeText(this, "in show", 5000).show();
SQLHelper myh = new SQLHelper(this,dbversion);
SQLiteCursor cc=(SQLiteCursor) myh.read();
//readDB();
Log.d("Report5"," in report");
String text = new String();
try{
if(cc != null){
cc.moveToFirst();
text = cc.getString(1);
}
else
{
text = "bad";
}
}catch(Exception e){
Toast.makeText(this, e.getMessage(), 5000);
}
Log.d("report data base", text );
/*while (!cc.isAfterLast()){
String t = cc.getString(cc.getColumnIndexOrThrow(SQLHelper.ITEM));
text = text + t;
}*/
//Toast.makeText(this, "everthing ok", 5000).show();
}
答案 0 :(得分:0)
请检查游标总是不应为null,为此目的使用以下代码...
if (cursor != null)
cursor.moveToFirst();
你关闭了
db.close();
在获取游标之前。关闭游标后关闭数据库
请发送有关logcat和确切问题的更多信息