gpt4 book ai didi

java - 从数据库检索数据?

转载 作者:行者123 更新时间:2023-12-02 11:32:14 24 4
gpt4 key购买 nike

我有一个 SQLite 数据库,当用户创建配置文件并在 SetUpProfile Activity 中填写其联系信息的表单时,该数据库就会被填充,然后检索该数据并将其显示在 UserProfile Activity 。

问题:如何在 TextView 中显示个人资料数据(姓名、电话、地址...)?

这是我到目前为止所做的:

配置文件类别

public class Profile {
private int _id = 1;
private String _industryName;
private String _industryType;
private String _email;
private String _phone;
private String _address;
private String _packageType;

public Profile() {
/** stays empty */
}

public Profile(int _id, String _industryName, String _industryType, String _email, String _phone, String _address, String _packageType) {
this._id = _id;
this._industryName = _industryName;
this._industryType = _industryType;
this._email = _email;
this._phone = _phone;
this._address = _address;
this._packageType = _packageType;
}

public Profile(String _industryName, String _industryType, String _email, String _phone, String _address, String _packageType) {
this._industryName = _industryName;
this._industryType = _industryType;
this._email = _email;
this._phone = _phone;
this._address = _address;
this._packageType = _packageType;
}
//getters and setters
}

这是处理程序DBHandler类

addProfile() 方法:

    public void addProfile(Profile profile) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_INDUSTRY_NAME, profile.get_industryName());
values.put(KEY_INDUSTRY_TYPE, profile.get_industryType());
values.put(KEY_EMAIL, profile.get_email());
values.put(KEY_PHONE, profile.get_phone());
values.put(KEY_ADDRESS, profile.get_address());
values.put(KEY_PACKAGE_TYPE, profile.get_packageType());

// Inserting Row
db.insert(TABLE_PROFILE, null, values);
db.close();
}

getProfile()方法

public Profile getProfile(int id) {
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_PROFILE, new String[] {
KEY_ID, KEY_INDUSTRY_NAME, KEY_INDUSTRY_TYPE, KEY_EMAIL, KEY_PHONE, KEY_ADDRESS, KEY_PACKAGE_TYPE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();

Profile profile = new Profile(
Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getString(6)
);

return profile;
}

MainActivity 类

public class UserProfile extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);

//SQLite databes is supposed to populate those textViews
industry_type = (TextView) findViewById(R.id.b_industry);
b_name = (TextView) findViewById(R.id.b_industry_name);
b_email = (TextView) findViewById(R.id.mail);
b_phone = (TextView) findViewById(R.id.phone);
b_address = (TextView) findViewById(R.id.address);
plan_type = (TextView) findViewById(R.id.p_title);

edit_profile = (Button) findViewById(R.id.editProfile);

industry_img = (ImageView) findViewById(R.id.thumbnail);
plan_img = (ImageView) findViewById(R.id.plan_icon);


edit_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditProfile();
}
});
}

最佳答案

这是我使用的方法:

SQLiteDatabase db = null;
DBHandler dd = new DBHandler(getBaseContext());
dd.getWritableDatabase();
db= openOrCreateDatabase("profileManager.db", Context.MODE_PRIVATE, null);
Cursor cc = db.rawQuery("SELECT * FROM profile", null);
if(cc.getCount()>=1) {
cc.moveToFirst();
try {
for(int i=0;i < cc.getCount();i++){
bname = cc.getString(1);
btype = cc.getString(2);
bmail = cc.getString(3);
bphone = cc.getString(4);
baddress = cc.getString(5);
bpackage = cc.getString(6);
}
}catch (Exception e){
e.getMessage();
}
}

关于java - 从数据库检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49209187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com