这是我的sqlite数据库代码
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"+ SERVER_ID + " TEXT,"+ ACCESS_TOKEN + " TEXT," + FIRST_NAME
+ " TEXT," + LAST_NAME + " TEXT," + USER_EMAIL + " TEXT,"
+ USERNAME + " TEXT," + ADDRESS + " TEXT," + LATITUDE + " TEXT,"
+ LONGITUDE + " TEXT," + BLOOD_GROUP + " TEXT," + GENDER + " TEXT,"
+ PHONE_NUMBER + " TEXT," + MOBILE_NUMBER + " TEXT," + DOB_YEAR + " TEXT,"
+ DOB_MONTH + " TEXT," + DOB_DAY + " TEXT," + COUNTRY + " TEXT," + IMAGE_URL
+ " TEXT," + TYPE + " TEXT" +");";
db.execSQL(CREATE_LOGIN_TABLE);
这是我从表中获取所有值的代码
public HashMap<String, String> getBPositiveData(){
HashMap<String, String> getData = new HashMap<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from user where type =? and blood_group =?", new String[]{"friends", "B_plus"});
//Move to first
if (cursor.moveToFirst()){
do {
getData.put("server_id", cursor.getString(1));
getData.put("first_name", cursor.getString(3));
getData.put("last_name", cursor.getString(4));
getData.put("blood_group", cursor.getString(10));
getData.put("mobile_number", cursor.getString(13));
getData.put("imageURL", cursor.getString(18));
cursor.moveToNext();
int abc = cursor.getCount();
Log.e("cursorCount", String.valueOf(abc));
}while (!cursor.isAfterLast());
cursor.close();
}
return getData;
}
在我的数据库中,所有行都已正确插入,查询也为我提供了所需的数据(我从genymotion模拟器中检查了数据库,因此我确信数据存在于那里)。我的问题是我只从该表的最后一行获取值。谁能告诉我,我在这里做错了什么?我用来检索活动中的值的代码是:
HashMap<String, String> profile = db.getBPositiveData();
final String fname = profile.get("first_name");
final String lname = profile.get("last_name");
final String bgroup = profile.get("blood_group");
final String mnumber = profile.get("mobile_number");
final String iurl = profile.get("imageURL");
提前谢谢你。
答案 0 :(得分:0)
尝试你的&#34; if(cursor.moveToFirst())&#34;作为&#34; while(cursor.next()&#34;循环并在循环中返回你的getData。
我在谈论的一个例子可以在adding data to hashmap from database中看到。
答案 1 :(得分:0)
对于每次迭代,你都会在Hashmap中覆盖你的dara。例如,您只能有一个键“server_id”。对于您的代码放置的第一条记录
getData.put(“server_id”,cursor.getString(1));
当您拨打
getData.put(“server_id”,cursor.getString(1));
它覆盖了。
答案 2 :(得分:0)
创建一个新类User
公共类用户{
String first_name;
String last_name;
String blood_group;
// ...the rest of the propertied
}
更改getData的HashMap声明
HashMap userList = new HashMap&lt;&gt;();
然后修改代码
User currentUser = null;
if(cursor.moveToFirst())
{
do {
currentUser = new User();
currentUser.first_name = cursor.getString(3);
currentUser.last_name = cursor.getString(4);
currentUser.blood_group = cursor.getString(10);
// the rest of your data for user
String rowKey = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
userList.put(rowKey, currentUser);
cursor.moveToNext();
// the rest of your code
} while (!cursor.isAfterLast());
cursor.close();
return userList;
}
然后相应地将方法的返回类型从HashMap更改为HashMap。
如果这对您有用,请告诉我
答案 3 :(得分:0)
首先,你需要创建你的对象类,例如。 User
public class User {
private String firstName;
private String lastName;
private String bloodGroup;
private String mobileNumber;
private String imageUrl;
public User() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
然后为用户提供B-positive用户:
public List<User> getBPositiveUsers() {
List<User> users = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from user where type =? and blood_group =?", new String[]{"friends", "B_plus"});
try {
if (cursor.moveToFirst()) {
do {
User user = new User();
user.setFirstName(cursor.getString(3));
user.setLastName(cursor.getString(4));
user.setBloodGroup(cursor.getString(10));
user.setMobileNumber(cursor.getString(13));
user.setImageUrl(cursor.getString(18));
users.add(user);
} while(cursor.moveToNext());
}
} catch (Exception e) {
Log.d("ERROR", "Error while trying to get users from database");
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return users;
}
使用光标时,try-catch-finally块总是很好。
user.setFirstName(cursor.getString(3));
使用起来很丑陋。
如果您将列名称作为常量(我假设您正在使用它,因为在创建时使用ex。FIRST_NAME
),如果您更改此行并且其他类似于user.setFirstName(cursor.getString(cursor.getColumnIndex(FIRST_NAME)));
检查Lombok是否为变量创建getter和setter。它使代码更容易阅读:)