我尝试使用以下查询创建一个sqlite表。
"CREATE TABLE " + TABLE_COMPLIANCE_STATUS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_YEAR + " TEXT,"
+ KEY_DELAYED_COMPLIANCE + " TEXT,"
+ KEY_NOT_COMPLIED + " TEXT" + ")";
我使用以下代码将值添加到db
db.addComplianceStatus(new ComplianceStatusModel(i, String.valueOf(year), String.valueOf(delayedCompliance), String.valueOf(notCompliance)));
我正在阅读db的内容,如下所示,
List<ComplianceStatusModel> notifications = db.getAllComplianceStatus();
for (ComplianceStatusModel notify : notifications) {
String log = "Id: "+notify.getId()+" ,Description: " + notify.getYear() + " ,Date: " + notify.getDelayedCompliance();
// Writing Compliances to log
Toast.makeText(getActivity().getApplicationContext(),log,Toast.LENGTH_SHORT).show();
}
这将返回一个空数组。可能是我的错误?当我尝试打印记录数据库时,它会打印内存地址。
以下是我的getAllComplianceModel方法
public List<ComplianceStatusModel> getAllComplianceStatus() {
List<ComplianceStatusModel> complianceStatusModelList = new ArrayList<ComplianceStatusModel>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_COMPLIANCE_STATUS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
ComplianceStatusModel complianceStatusModel = new ComplianceStatusModel();
complianceStatusModel.setId(Integer.parseInt(cursor.getString(0)));
complianceStatusModel.setYear(cursor.getString(1));
complianceStatusModel.setDelayedCompliance(cursor.getString(2));
complianceStatusModel.setNotComplied(cursor.getString(3));
complianceStatusModelList.add(complianceStatusModel);
} while (cursor.moveToNext());
}
return complianceStatusModelList;
}
以下是addComplianceStatus方法
public void addComplianceStatus(ComplianceStatusModel complianceStatus) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, complianceStatus.getId());
contentValues.put(KEY_YEAR, complianceStatus.getYear());
contentValues.put(KEY_DELAYED_COMPLIANCE, complianceStatus.getDelayedCompliance());
contentValues.put(KEY_NOT_COMPLIED, complianceStatus.getNotComplied());
}
答案 0 :(得分:1)
您在括号前的create table语句中缺少空格: TABLE_COMPLIANCE_STATUS +&#34;(&#34; - &gt; TABLE_COMPLIANCE_STATUS +&#34;(&#34;
答案 1 :(得分:1)
你在创建sqlite语句中错过了分号
"CREATE TABLE " + TABLE_COMPLIANCE_STATUS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_YEAR + " TEXT,"
+ KEY_DELAYED_COMPLIANCE + " TEXT,"
+ KEY_NOT_COMPLIED + " TEXT" + ");";
测试数据库是否已创建。
DatabaseHelper dbHelper = new DatabaseHelper(mockContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Log.d("my_db_test", dbHelper.getDatabaseName()));
测试数据库版本。 (从前面的代码继续)
Log.d("my_db_test", db.getVersion()));
测试表中的列(从前面的代码继续)
Cursor dbCursor = db.query(YOUR_TABLE_NAME, null, null, null, null, null, null);
String[] columnNames = dbCursor.getColumnNames();
Log.d("my_db_test", "columns names: \n");
for(int i = 0; i < columnNames.length; i++){
Log.d("my_db_test", "columnNames " + i + ": " + columnNames[i] + "\n");
}
如果所有测试都通过,则表格会正确创建。然后你应该检查保存和加载方法。
我使用下面的代码将记录保存在数据库中
ContentValues values = new ContentValues();
values.put(DatabaseHelper.P_NAME, person.getName());
values.put(DatabaseHelper.P_AGE, person.getAge());
values.put(DatabaseHelper.P_COUNTRY, person.getCountry());
isAdded = db.insert(YOUR_TABLE_NAME, null, values) > 0;
并使用代码加载所有记录
Cursor cursor = db.query(YOUR_TABLE_NAME, null, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Person p = new Person();
p.setId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.P_ID)));
p.setName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.P_NAME)));
p.setAge(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.P_AGE)));
p.setCountry(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.P_COUNTRY)));
personList.add(p);
cursor.moveToNext();
}
答案 2 :(得分:1)
您尚未添加将值存储在表格中的方法。在您的addCompilanceStatus()
方法中。你需要添加:
public void addComplianceStatus(ComplianceStatusModel complianceStatus) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, complianceStatus.getId());
contentValues.put(KEY_YEAR, complianceStatus.getYear());
contentValues.put(KEY_DELAYED_COMPLIANCE, complianceStatus.getDelayedCompliance());
contentValues.put(KEY_NOT_COMPLIED, complianceStatus.getNotComplied());
sqLiteDatabase.insert(TABLE_COMPLIANCE_STATUS, null, contentValues); //YOU HAVE FORGOT THIS
}