将值插入到正确插入的数据库中。但在尝试检索该值时,数据库字段名称正在获取。到目前为止,我已完成以下代码,
String REPORT_PROBLEM_DATABASE_LIST = "create table if not exists " + REPORT_PROBLEM_DATABASE_LIST_TABLE_NAME + "(_id integer primary key autoincrement,userId text not null,storeId text not null,machineDeviceId text not null,machineName text not null,machineIp text not null,softwareId text not null,contentDescDetails text not null,gps text not null,attachments text not null)";
db.execSQL(REPORT_PROBLEM_DATABASE_LIST);
public static void ReportProblemDatabaseListAddToDB(SQLiteDatabase db, ContentValues values, String userId, String storeId, String machineDeviceId, String machineName, String machineIp, String softwareId,String contentDescDetails,String gps,String attachments) {
values.put("userId", userId);
values.put("storeId", storeId);
values.put("machineDeviceId", machineDeviceId);
values.put("machineName", machineName);
values.put("machineIp", machineIp);
values.put("softwareId", softwareId);
values.put("contentDescDetails",contentDescDetails);
values.put("gps",gps);
values.put("attachments",attachments);
db.insert(Database.REPORT_PROBLEM_DATABASE_LIST_TABLE_NAME, null, values);
}
在我的按钮中单击“插入值
”if (!db.isOpen()) {
DBHelper = new Database(getActivity(), "Database", null, 1);
db = DBHelper.getWritableDatabase();
}
reportProblemDatabaseModelsList.clear();
String descriptions = rp_descriptionET.getText().toString();
String attachments = String.valueOf(attachmentModelsList);
ReportProblemDatabaseModel reportProblemDatabaseModel = new ReportProblemDatabaseModel(UserID, storeIdSpValue,
machineDeviceIdSend, machineNameAT.getText().toString(),
rp_ipET.getText().toString(), softWareIdSend, descriptions,
gpsSend, attachments);
reportProblemDatabaseModelsList.add(reportProblemDatabaseModel);
String selectQuery = "SELECT * FROM REPORT_PROBLEM_DATABASE_LIST_TABLE";
Cursor cursor = db.rawQuery(selectQuery, null);
ContentValues values = new ContentValues();
DBUtil.ReportProblemDatabaseListAddToDB(db, values, UserID, storeIdSpValue, machineDeviceIdSend,
machineNameAT.getText().toString(), rp_ipET.getText().toString(), softWareIdSend, descriptions, gpsSend, attachments);
db.close();
Intent intent = new Intent(getActivity(), ReportProblemDatabaseActivity.class);
startActivity(intent);
检索数据时,
public void savedItems() {
String selectQuery = "SELECT * FROM REPORT_PROBLEM_DATABASE_LIST_TABLE";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToFirst()) {
do {
String UserID = cursor.getString(1);
String storeIdSpValue = cursor.getString(2);
String machineDeviceIdSend = cursor.getString(3);
String machineName = cursor.getString(4);
String machineIp = cursor.getString(5);
String softWareIdSend = cursor.getColumnName(6);
String description = cursor.getColumnName(7);
Log.e("descriptionDb",description);
String gps = cursor.getColumnName(8);
String attachments = cursor.getColumnName(9);
// String attachmentsLists = String.valueOf(attachmentsList);
ReportProblemDatabaseModel reportProblemDatabaseModel = new ReportProblemDatabaseModel(UserID, storeIdSpValue,
machineDeviceIdSend, machineName,
machineIp, softWareIdSend, description,
gps, attachments);
reportProblemDatabaseModelsList.add(reportProblemDatabaseModel);
} while (cursor.moveToNext());
}
}
答案 0 :(得分:1)
String description = cursor.getColumnName(7);
getColumnName()
获取列的名称。
要获取第八列中的值,您必须使用getString(7)
。
答案 1 :(得分:1)
根据documentation,cursor.getColumnName()
返回
给定的从零开始的列索引的列名。
因此,替换
String description = cursor.getColumnName(7);
与
String description = cursor.getString(7);