android studio 上的 SQLite 为 ID 分配随机数并且不更新
SQLite on android studio assigns random numbers to ID and doesn'tt update
在过去的一个小时里一直在努力寻找问题,但它已经失控了,我的应用程序可以读取数据,我使用显示所有信息将其放入字符串的 ArrayList 中,这看起来很准确,然后当我尝试更新新数据是在问题开始出现时,其中最大的问题是当我看到数据库中实际存在的内容时
编辑:真正的问题是它们不是以 ID 1、2、3、4、5 开始的
如您所见,它们并不是真正的 "random",但它们从 id 进行的跳转是,或者至少看起来是,因为它们从 21 开始,也许这是我的删除方法?
public void deleteAll()
{
SQLiteDatabase db = this.getWritableDatabase();
// db.delete(TABLE_NAME,null,null);
//db.execSQL("delete * from"+ TABLE_NAME);
db.execSQL("delete from "+ TABLE_NAME);
}
这是我的代码,也许有人可以帮我解决这个问题,谢谢,如果这个问题打扰了你,我很抱歉,我总是试着把这里作为最后的资源来问。
public class AmmunitionDbHelper extends SQLiteOpenHelper {
/*
Nombre de la BBDD
*/
private static final String DATABASE_NAME = "ammunition.db";
/*
Version actual de la BBDD
Si cambiamos el esquema actualizamos el numero
*/
private static final int DATABASE_VERSION = 3;
public AmmunitionDbHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_PETS_TABLE = "CREATE TABLE " + TABLE_NAME +
"("
+ AmmunitionEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ AmmunitionEntry.COLUMN_TORPEDO_QUANTITY + " TEXT NOT NULL);";
db.execSQL(SQL_CREATE_PETS_TABLE);
}
public ArrayList<String> displayDatabaseInfo() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> ammunition = new ArrayList<String>();
//La projection nos indica las columnas que consultamos
String[] projection = {
_ID,
AmmunitionEntry.COLUMN_TORPEDO_QUANTITY
};
Cursor cursor = db.query(
TABLE_NAME,
projection,
null,
null,
null,
null,
null
);
//obtenemos indices de las columnas
int quantityColumn = cursor.getColumnIndex(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY);
//Con cada uno de los indices ya podemos recorrer las filas
while(cursor.moveToNext()) {
String currentQuantity = cursor.getString(quantityColumn);
ammunition.add(currentQuantity);
}
return ammunition;
}
public long insertAmmunition(String quantity) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY, quantity);
long newRowId = db.insert(TABLE_NAME, null, contentValues);
return newRowId;
}
public String insertOneAmmunition(String quantity, String id) {
SQLiteDatabase db = this.getReadableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY, quantity);
// Which row to update, based on the ID
String selection = _ID + " LIKE ?";
String[] selectionArgs = { id };
try {
int count = db.update(
TABLE_NAME,
values,
selection,
selectionArgs);
} catch (Exception e) {
e.printStackTrace();
}
return id;
}
public String displayAmmo(String id) {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE "+_ID + " = '" + id +"'";
//Cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results
c.moveToFirst();
dbString = c.getString(c.getColumnIndex(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY));
return dbString;
}
public void deleteAll()
{
SQLiteDatabase db = this.getWritableDatabase();
// db.delete(TABLE_NAME,null,null);
//db.execSQL("delete * from"+ TABLE_NAME);
db.execSQL("delete from "+ TABLE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
这是我尝试更新一些值的地方
switch (v.getId()) {
case R.id.plusammunition1:
valor = Integer.parseInt(torpedo1.getText().toString());
valor++;
if (valor >= 10) {
torpedo1.setText("10");
} else {
torpedo1.setText(String.valueOf(valor));
}
String test = mDbHelper.insertOneAmmunition(String.valueOf(valor), "51");
Log.e("Value info 1", "Hey " + test);
Log.e("INFO 1", "Hoy " + mDbHelper.displayDatabaseInfo().get(0));
valor = 0;
break;
这是以防万一的合同。
public static final class AmmunitionEntry implements BaseColumns {
public final static String TABLE_NAME = "Torpedo";
public final static String _ID = BaseColumns._ID;
public final static String COLUMN_TORPEDO_QUANTITY = "quantity";
}
我猜更新的问题是因为 ID
当您定义这样的列时:
ID INTEGER PRIMARY KEY
则此列将自动递增,但如果您删除任何行,则 table 中缺失的 id
可能会在将来通过插入重新填充。
如果您还像这样在定义的末尾添加关键字 AUTOINCREMENT
:
ID INTEGER PRIMARY KEY AUTOINCREMENT
这可以防止重复使用 id
的任何已删除值,因此您将拥有永久性的空白。
您可以在这里找到更多信息:SQLite Autoincrement
在过去的一个小时里一直在努力寻找问题,但它已经失控了,我的应用程序可以读取数据,我使用显示所有信息将其放入字符串的 ArrayList 中,这看起来很准确,然后当我尝试更新新数据是在问题开始出现时,其中最大的问题是当我看到数据库中实际存在的内容时
编辑:真正的问题是它们不是以 ID 1、2、3、4、5 开始的
如您所见,它们并不是真正的 "random",但它们从 id 进行的跳转是,或者至少看起来是,因为它们从 21 开始,也许这是我的删除方法?
public void deleteAll()
{
SQLiteDatabase db = this.getWritableDatabase();
// db.delete(TABLE_NAME,null,null);
//db.execSQL("delete * from"+ TABLE_NAME);
db.execSQL("delete from "+ TABLE_NAME);
}
这是我的代码,也许有人可以帮我解决这个问题,谢谢,如果这个问题打扰了你,我很抱歉,我总是试着把这里作为最后的资源来问。
public class AmmunitionDbHelper extends SQLiteOpenHelper {
/*
Nombre de la BBDD
*/
private static final String DATABASE_NAME = "ammunition.db";
/*
Version actual de la BBDD
Si cambiamos el esquema actualizamos el numero
*/
private static final int DATABASE_VERSION = 3;
public AmmunitionDbHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_PETS_TABLE = "CREATE TABLE " + TABLE_NAME +
"("
+ AmmunitionEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ AmmunitionEntry.COLUMN_TORPEDO_QUANTITY + " TEXT NOT NULL);";
db.execSQL(SQL_CREATE_PETS_TABLE);
}
public ArrayList<String> displayDatabaseInfo() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> ammunition = new ArrayList<String>();
//La projection nos indica las columnas que consultamos
String[] projection = {
_ID,
AmmunitionEntry.COLUMN_TORPEDO_QUANTITY
};
Cursor cursor = db.query(
TABLE_NAME,
projection,
null,
null,
null,
null,
null
);
//obtenemos indices de las columnas
int quantityColumn = cursor.getColumnIndex(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY);
//Con cada uno de los indices ya podemos recorrer las filas
while(cursor.moveToNext()) {
String currentQuantity = cursor.getString(quantityColumn);
ammunition.add(currentQuantity);
}
return ammunition;
}
public long insertAmmunition(String quantity) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY, quantity);
long newRowId = db.insert(TABLE_NAME, null, contentValues);
return newRowId;
}
public String insertOneAmmunition(String quantity, String id) {
SQLiteDatabase db = this.getReadableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY, quantity);
// Which row to update, based on the ID
String selection = _ID + " LIKE ?";
String[] selectionArgs = { id };
try {
int count = db.update(
TABLE_NAME,
values,
selection,
selectionArgs);
} catch (Exception e) {
e.printStackTrace();
}
return id;
}
public String displayAmmo(String id) {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE "+_ID + " = '" + id +"'";
//Cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results
c.moveToFirst();
dbString = c.getString(c.getColumnIndex(AmmunitionEntry.COLUMN_TORPEDO_QUANTITY));
return dbString;
}
public void deleteAll()
{
SQLiteDatabase db = this.getWritableDatabase();
// db.delete(TABLE_NAME,null,null);
//db.execSQL("delete * from"+ TABLE_NAME);
db.execSQL("delete from "+ TABLE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
这是我尝试更新一些值的地方
switch (v.getId()) {
case R.id.plusammunition1:
valor = Integer.parseInt(torpedo1.getText().toString());
valor++;
if (valor >= 10) {
torpedo1.setText("10");
} else {
torpedo1.setText(String.valueOf(valor));
}
String test = mDbHelper.insertOneAmmunition(String.valueOf(valor), "51");
Log.e("Value info 1", "Hey " + test);
Log.e("INFO 1", "Hoy " + mDbHelper.displayDatabaseInfo().get(0));
valor = 0;
break;
这是以防万一的合同。
public static final class AmmunitionEntry implements BaseColumns {
public final static String TABLE_NAME = "Torpedo";
public final static String _ID = BaseColumns._ID;
public final static String COLUMN_TORPEDO_QUANTITY = "quantity";
}
我猜更新的问题是因为 ID
当您定义这样的列时:
ID INTEGER PRIMARY KEY
则此列将自动递增,但如果您删除任何行,则 table 中缺失的 id
可能会在将来通过插入重新填充。
如果您还像这样在定义的末尾添加关键字 AUTOINCREMENT
:
ID INTEGER PRIMARY KEY AUTOINCREMENT
这可以防止重复使用 id
的任何已删除值,因此您将拥有永久性的空白。
您可以在这里找到更多信息:SQLite Autoincrement