Android SQLite 游标在尝试读取所有条目时只返回一个条目
Android SQLite Cursor returning only one entry when trying to read all entries
我正在尝试从 SQLite 数据库中读取所有条目 table。问题是游标 returns 每次只有一个条目。我知道 table 已经有大约 30 个条目,因为当我再次尝试保存条目时,每个条目都会出现此错误:
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: list.id (code 1555)
#################################################################
Error Code : 1555 (SQLITE_CONSTRAINT_PRIMARYKEY)
Caused By : Abort due to constraint violation.
(UNIQUE constraint failed: list.id (code 1555))
#################################################################
我在这里看到了几个关于类似问题的问题,但是 none 的解决方案对我有用。到目前为止,这是我尝试过的:
public class Table
{
private static final String TABLE_NAME = "list";
// Column names
private static final String ID = "id";
private static final String INDEX = "index";
private static final String TITLE = "title";
private static final String CREATE_STATEMENT = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
"(" + ID + " INTEGER PRIMARY KEY, " + TITLE + " TEXT, " +
INDEX + " INTEGER)";
public static void create(SQLiteDatabase db)
{
db.execSQL(CREATE_STATEMENT);
}
public static void drop(SQLiteDatabase db)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public static void saveList(SQLiteDatabase db, ArrayList<MyObject> list)
{
for (MyObject obj : list)
{
long id = insert(db, obj);
}
}
private static long insert(SQLiteDatabase db, MyObject obj)
{
final ContentValues values = new ContentValues();
values.put(ID, obj.getId());
values.put(INDEX, obj.getIndex());
values.put(TITLE, obj.getTitle());
return db.insert(TABLE_NAME, null, values);
}
public static ArrayList<MyObject> loadList(SQLiteDatabase db)
{
final ArrayList<MyObject> list = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
Log.d(TABLE_NAME, "Cursor size: " + cursor.getCount());
if (cursor.moveToFirst())
{
do
{
final long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
final String title = cursor.getString(cursor.getColumnIndexOrThrow(TITLE));
final int index = cursor.getInt(cursor.getColumnIndexOrThrow(INDEX));
list.add(new MyObject(id, index, title);
}
while (cursor.moveToNext());
}
cursor.close();
return list;
}
}
我也尝试过使用 cursor = db.query(TABLE_NAME, null, null, ..., null)
但得到了相同的结果。
这就是我从 SQLiteOpenHelper
class 中调用上述函数的方式:
public void saveList(ArrayList<MyObj> list)
{
final SQLiteDatabase db = getWritableDatabase();
Table.drop(db);
Table.create(db);
Table.saveList(db, list);
}
public ArrayList<MyObject> loadList()
{
return Table.loadList(getReadableDatabase());
}
我在同一个数据库中还有另一个 table,它的代码非常相似,但工作正常。一段时间以来,我一直在尝试解决这个问题,但我看不出发生了什么。任何帮助将不胜感激
您的
有问题
obj.getId()
private static long insert(SQLiteDatabase db, MyObject obj)
{
final ContentValues values = new ContentValues();
values.put(ID, obj.getId()); // check whether you are updating this with different value or not
values.put(INDEX, obj.getIndex());
values.put(TITLE, obj.getTitle());
return db.insert(TABLE_NAME, null, values);
}
我曾经遇到过同样的问题,尝试将一个 id 字段(_id INTEGER PRIMARY KEY AUTOINCREMENT)添加到 table,一旦我添加了该字段,我就能够根据需要检索更多行
我正在尝试从 SQLite 数据库中读取所有条目 table。问题是游标 returns 每次只有一个条目。我知道 table 已经有大约 30 个条目,因为当我再次尝试保存条目时,每个条目都会出现此错误:
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: list.id (code 1555)
#################################################################
Error Code : 1555 (SQLITE_CONSTRAINT_PRIMARYKEY)
Caused By : Abort due to constraint violation.
(UNIQUE constraint failed: list.id (code 1555))
#################################################################
我在这里看到了几个关于类似问题的问题,但是 none 的解决方案对我有用。到目前为止,这是我尝试过的:
public class Table
{
private static final String TABLE_NAME = "list";
// Column names
private static final String ID = "id";
private static final String INDEX = "index";
private static final String TITLE = "title";
private static final String CREATE_STATEMENT = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
"(" + ID + " INTEGER PRIMARY KEY, " + TITLE + " TEXT, " +
INDEX + " INTEGER)";
public static void create(SQLiteDatabase db)
{
db.execSQL(CREATE_STATEMENT);
}
public static void drop(SQLiteDatabase db)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public static void saveList(SQLiteDatabase db, ArrayList<MyObject> list)
{
for (MyObject obj : list)
{
long id = insert(db, obj);
}
}
private static long insert(SQLiteDatabase db, MyObject obj)
{
final ContentValues values = new ContentValues();
values.put(ID, obj.getId());
values.put(INDEX, obj.getIndex());
values.put(TITLE, obj.getTitle());
return db.insert(TABLE_NAME, null, values);
}
public static ArrayList<MyObject> loadList(SQLiteDatabase db)
{
final ArrayList<MyObject> list = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
Log.d(TABLE_NAME, "Cursor size: " + cursor.getCount());
if (cursor.moveToFirst())
{
do
{
final long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
final String title = cursor.getString(cursor.getColumnIndexOrThrow(TITLE));
final int index = cursor.getInt(cursor.getColumnIndexOrThrow(INDEX));
list.add(new MyObject(id, index, title);
}
while (cursor.moveToNext());
}
cursor.close();
return list;
}
}
我也尝试过使用 cursor = db.query(TABLE_NAME, null, null, ..., null)
但得到了相同的结果。
这就是我从 SQLiteOpenHelper
class 中调用上述函数的方式:
public void saveList(ArrayList<MyObj> list)
{
final SQLiteDatabase db = getWritableDatabase();
Table.drop(db);
Table.create(db);
Table.saveList(db, list);
}
public ArrayList<MyObject> loadList()
{
return Table.loadList(getReadableDatabase());
}
我在同一个数据库中还有另一个 table,它的代码非常相似,但工作正常。一段时间以来,我一直在尝试解决这个问题,但我看不出发生了什么。任何帮助将不胜感激
您的
有问题obj.getId()
private static long insert(SQLiteDatabase db, MyObject obj)
{
final ContentValues values = new ContentValues();
values.put(ID, obj.getId()); // check whether you are updating this with different value or not
values.put(INDEX, obj.getIndex());
values.put(TITLE, obj.getTitle());
return db.insert(TABLE_NAME, null, values);
}
我曾经遇到过同样的问题,尝试将一个 id 字段(_id INTEGER PRIMARY KEY AUTOINCREMENT)添加到 table,一旦我添加了该字段,我就能够根据需要检索更多行