我无法在SQLite数据库中插入多条记录。我已经查询了类似的问题,但我无法找到错误。
以下是我的CustomSQLiteHelper的一部分:
@Override
public void onCreate(SQLiteDatabase db) {
final String CREATE_DATABASE_RESOURCE =
"CREATE TABLE Resource("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"timestamp TEXT UNIQUE, " +
"title TEXT, " +
"description TEXT, " +
"latitude TEXT NOT NULL, " +
"longitude TEXT NOT NULL, " +
"altitude TEXT NOT NULL);"; //+
//"PRIMARY KEY (_id, timestamp));";
final String CREATE_DATABASE_IMAGE =
"CREATE TABLE Image("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"timestamp TEXT UNIQUE, " +
"timestampR TEXT NOT NULL, " +
"path TEXT NOT NULL);";// +
//"PRIMARY KEY (_id, timestamp, timestampR), " +
//"FOREIGN KEY (timestampR) REFERENCES Resource (timestamp));";
db.execSQL(CREATE_DATABASE_RESOURCE);
db.execSQL(CREATE_DATABASE_IMAGE);
}
public void insertResourceMetadata(String timestamp, String title, String desc, String lat, String lng, String alt){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("timestamp", timestamp);
values.put("title", title);
values.put("description", desc);
values.put("latitude", lat);
values.put("longitude", lng);
values.put("altitude", alt);
database.insert("Resource", null, values);
database.close();
}
public void insertImageMetadata(String timestamp, String timestampR, String path){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues tempValues = new ContentValues();
tempValues.put("timestamp", timestamp);
tempValues.put("timestampR", timestampR);
tempValues.put("path", path);
database.insert("Image", null, tempValues);
database.close();
}
我有一个场景,其中insertImageMetadata在for循环中被调用三次,但在数据库内部始终只是第一个记录的条目(根本)。