尝试从SQLitedatabase获取数据,但要遵循errer:
android.database.CursorIndexOutOfBoundExceptions: Index -1 requested with a size of 4
指导我这个错误正在发生。提前谢谢
这是我所做的所有代码:
DataBaseHelper.java
public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to "
+_newVersion + ", which will destroy all old data");
_db.execSQL("DROP TABLE IF EXISTS " + "LOGIN2");
onCreate(_db);
}
}
LoginDataBaseHelper.java
public class LoginDataBaseAdapter
{
ArrayList<String> results=new ArrayList<String>();
static final String DATABASE_NAME = "login2.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
public static final String Key_id="_id";
static final String DATABASE_CREATE = "create table "+"LOGIN2"+
"( " +"Key_id"+" integer primary key autoincrement,"
+ "NAME text, FATHERNAME text, MOTHERNAME text,
DOB integer, ADDRESS text, PHONE integer, GENDER text,
MARITALSTATUS text, EMAIL text, USERNAME text,
PASSWORD text); ";
public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String name,String father,
String mother, String dob, String address, String phone,
String gender,String marital,String email,String username,
String password )
{
ContentValues newValues = new ContentValues();
newValues.put("NAME", name);
newValues.put("FATHERNAME",father);
newValues.put("MOTHERNAME", mother);
newValues.put("DOB", dob);
newValues.put("ADDRESS", address);
newValues.put("PHONE", phone);
newValues.put("GENDER", gender);
newValues.put("MARITALSTATUS", marital);
newValues.put("EMAIL", email);
newValues.put("USERNAME", username);
newValues.put("PASSWORD", password);
db.insert("LOGIN2", null, newValues);
}
public int deleteEntry(String Address)
{
String where="ADDRESS=?";
int numberOFEntriesDeleted=
db.delete("LOGIN2", where, new String[]{Address}) ;
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("LOGIN2", null, " USERNAME=?",
new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password, String roll)
{
ContentValues updatedValues = new ContentValues();
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
updatedValues.put("ROLL", roll);
String where="USERNAME = ?";
db.update("LOGIN2",updatedValues, where, new String[]{userName});
}
public ArrayList<String> getFriends()
{
ArrayList<String> result=new ArrayList<String>();
Cursor c=db.rawQuery("SELECT * from"+" LOGIN2"+" ", null);
do
{
String strr=c.getString(c.getColumnIndex("USERNAME"));
result.add(strr);
}while(c.moveToNext());
return result;
}}
我想根据列表视图中的USERNAME字段打印所有用户。这是我为它编写的代码。
List.java
public class List extends ListActivity
{
ListView list1;
LoginDataBaseAdapter loginDataBaseAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
ArrayList<String> from=loginDataBaseAdapter.getFriends();
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,from);
list1.setAdapter(adapter);
}}
获取CursorIndexOutOfBoundException。
答案 0 :(得分:0)
首先调用返回cursor.movetoFirst();
boolean value
方法
if(cursor.moveToFirst()){
do{
// cursor.getString(0);.......
}while(cursor.moveToNext());}
答案 1 :(得分:0)
如果你想迭代所有行,这将有所帮助:
Cursor c = db.query(TABLENAME, null, null, null, null, null, null);
while(c.moveToNext()) {
int id = c.getInt(c.getColumnIndex("_ID"));
}
或者您可以使用其他游标函数,例如moveToPosition()
来访问由id
更多信息:http://developer.android.com/reference/android/database/Cursor.html
答案 2 :(得分:0)
if(cursor.getCount()>0){
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String data=cursor.getString(columnindex);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
尝试此代码并检查第一个cursor.getCount()以查看值是否在游标中。