Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 删除SQLite数据库的顶部项会导致应用程序崩溃_Android_Sqlite_Cursor_Sql Delete - Fatal编程技术网

Android 删除SQLite数据库的顶部项会导致应用程序崩溃

Android 删除SQLite数据库的顶部项会导致应用程序崩溃,android,sqlite,cursor,sql-delete,Android,Sqlite,Cursor,Sql Delete,我正在使用一个我在网上找到的SQLite数据库类。当我删除最上面的项并尝试重新加载数据库时,应用程序崩溃,因为数据库没有重新设计ID。这意味着对于ID为1和2的两个项目,如果删除第一个项目,第二个项目将保持ID为2。因此,当光标试图访问DB时,它调用ID为1的项(不再存在),应用程序崩溃。我对SQLite没有经验。我认为问题在于DB在删除一个项目后不会重新为其项目设计ID 我正在使用的SQLite代码 public class DBHelper extends SQLiteOpenHelper

我正在使用一个我在网上找到的SQLite数据库类。当我删除最上面的项并尝试重新加载数据库时,应用程序崩溃,因为数据库没有重新设计ID。这意味着对于ID为1和2的两个项目,如果删除第一个项目,第二个项目将保持ID为2。因此,当光标试图访问DB时,它调用ID为1的项(不再存在),应用程序崩溃。我对SQLite没有经验。我认为问题在于DB在删除一个项目后不会重新为其项目设计ID

我正在使用的SQLite代码

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "UsersDB.db";
public static final String USER_TABLE_NAME = "Users";
public static final String USER_COLUMN_ID = "id";
public static final String USER_COLUMN_NAME = "name";
public static final String USER_COLUMN_SURNAME = "surname";
public static final String USER_COLUMN_JOB_TITLE = "job_title";
public static final String USER_COLUMN_LEVEL = "level";
public static final String USER_COLUMN_ROLE = "role";
public static final String USER_COLUMN_PHONE = "phone";
private HashMap hp;

public DBHelper(Context context) {
    super(context, DATABASE_NAME , null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table Users " +
                    "(id integer primary key, name text, surname  text, job_title  text, level  text, role  text, phone  text)"
    );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS Users");
    onCreate(db);
}

public boolean insertUSER (String name, String surname, String job_title, String level, String role, String phone) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("surname", surname);
    contentValues.put("job_title", job_title);
    contentValues.put("level", level);
    contentValues.put("role", role);
    contentValues.put("phone", phone);
    db.insert("Users", null, contentValues);
    return true;
}

public Cursor getData(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from Users where id="+id+"", null );
    return res;
}

public int numberOfRows(){
    SQLiteDatabase db = this.getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, USER_TABLE_NAME);
    return numRows;
}

public boolean updateUSER (Integer id, String name, String surname, String job_title, String level, String role, String phone)  {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("surname", surname);
    contentValues.put("job_title", job_title);
    contentValues.put("level", level);
    contentValues.put("role", role);
    contentValues.put("phone", phone);
    db.update("Users", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
    return true;
}


public Integer deleteUSER (Integer id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete("Users",
            "id=?",
            new String[] { String.valueOf(id) });
}

public ArrayList<String> getAllUsers() {
    ArrayList<String> array_list = new ArrayList<String>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from Users", null );
    res.moveToFirst();

    while(!res.isAfterLast()){
        array_list.add(res.getString(res.getColumnIndex(USER_COLUMN_NAME)));
        res.moveToNext();
    }
    return array_list;
}
}
带着错误

Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
如果我将代码更改为cursor=userDB.getData(I+2);然后它正常工作,因为有一个ID为2的项

我的数据库有什么遗漏吗?我尝试使用db.exec(“真空”);删除后,但不起作用。
非常感谢您的帮助。

我认为问题在于数据库在删除一个项目后没有重新为其项目设计ID。这不是问题。这是正常的。列
id
不是行号。这是一个和其他专栏一样的专栏。因此,您的代码不能依赖于行id的值。好的,我明白了。我想我将存储一个行号,并在删除后手动重新分配它。谢谢您访问数据库的方式是错误的,您需要一个光标来循环浏览数据
cursor=userDB.getData(i+1);
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0