英文:
How to get data from SQLite database and display it in EditText
问题
如何从SQLite数据库获取数据并在EditText中显示它
我需要帮助从SQLite数据库中获取数据并在EditText中显示它。我已经编写了获取数据的代码,这正确吗?如何在EditText中显示数据?请帮助我。我需要从数据库中显示数据到EditText的确切代码。
SQLite数据库代码
public class DatabaseStudent extends SQLiteOpenHelper {
public static final String databasename="Student";
public static final String Student_table="Student_table";
public static final String Student_name="Name";
public static final String Student_rollnumber="RollNumber";
public static final String Student_College="College";
public static final String Student_Branch="Branch";
public static final String Student_Year="Year";
public static final String Parent_name="ParentName";
public static final String Parent_address="ParentAddress";
public static final String Parent_Phone_number="Phone";
public static final String Student_Email_ID="Email";
public static final String Student_Password="Password";
public static final int versioncode=1;
public DatabaseStudent(Context context){
super(
context,
databasename,
null,
versioncode);
}
@Override
public void onCreate(SQLiteDatabase database) {
String student_query;
student_query="CREATE TABLE IF NOT EXISTS "+Student_table+"(Name TEXT,RollNumber NUMBER PRIMARY KEY,College TEXT,Branch TEXT,Year TEXT,ParentName TEXT,ParentAddress TEXT,Phone NUMBER, Email Text, Password TEXT)";
database.execSQL(student_query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String student_query;
student_query= "DROP TABLE IF EXISTS " + Student_table;
database.execSQL(student_query);
}
public boolean Student_Data(String name,String rollnumber,String college,String branch,String year,String parentname,String parentaddress,String phone,String email,String password){
SQLiteDatabase db1=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(Student_name,name);
cv.put(Student_rollnumber,rollnumber);
cv.put(Student_College,college);
cv.put(Student_Branch,branch);
cv.put(Student_Year,year);
cv.put(Parent_name,parentname);
cv.put(Parent_address,parentaddress);
cv.put(Parent_Phone_number,phone);
cv.put(Student_Email_ID,email);
cv.put(Student_Password,password);
long result=db1.insert(Student_table,null,cv);
if(result==-1)
return false;
else
return true;
}
public Cursor StudentData()
{
SQLiteDatabase db1=this.getWritableDatabase();
Cursor res = db1.rawQuery("select * from "+Student_table,null);
return res;
}
}
java代码
public class StudentProfileActivity extends AppCompatActivity {
EditText editname,editroll,editcollege,editbranch,edityear,editparentname,editparentaddress,editphone,editemail,editpassword;
SQLiteDatabase sqLiteDatabase;
DatabaseStudent dbh;
Cursor res;
Student_Listadapter la;
Button btnupadte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_profile);
editname=findViewById(R.id.editname);
editroll=findViewById(R.id.editrollnumber);
editcollege=findViewById(R.id.editcollege);
editbranch=findViewById(R.id.editbranch);
edityear=findViewById(R.id.edityear);
editparentaddress=findViewById(R.id.editparentaddress);
editparentname=findViewById(R.id.editparentname);
editphone=findViewById(R.id.editphone);
editemail=findViewById(R.id.editemail);
editpassword=findViewById(R.id.editpassword);
btnupadte=findViewById(R.id.btnupdate);
dbh = new DatabaseStudent(getApplicationContext());
sqLiteDatabase=dbh.getReadableDatabase();
res=dbh.StudentData();
if(res.moveToFirst())
{
do{
String name,rollnumber,college,branch,year,parentname,parentaddress,phone,email,password;
name= res.getString(0);
rollnumber=res.getString(1);
college=res.getString(2);
branch=res.getString(3);
year=res.getString(4);
parentname=res.getString(5);
parentaddress=res.getString(6);
phone=res.getString(7);
email=res.getString(8);
password=res.getString(9);
Dataprovider_Student DPC= new Dataprovider_Student(name,rollnumber,college,branch,year,parentname,parentaddress,phone,email,password);
la.add(DPC);
}while(res.moveToNext());
}
btnupadte.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
xml代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/studentlist"
android:padding="10dp"
tools:context=".StudentProfileActivity">
<EditText
android:id="@+id/editname"
android:hint="Name"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editrollnumber"
android:hint="Roll number"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editcollege"
android:hint="college"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editbranch"
android:hint="branch"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edityear"
android:hint="year"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editparentname"
android:hint="parentname"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editparentaddress"
android:hint="parentaddress"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editphone"
android:hint="phone"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editemail"
android:hint="email"
android:ems="10"
android:layout_width="wrap_content"
<details>
<summary>英文:</summary>
How to get data from SQLite database and display it in EditText
I need help to get data from SQLite and display it in EditText I have coded for getdata is that correct and how can do the displaying part in EditText please help me. I need exact code for displaying the data into EditText from the database
thanks in advance
```
SQLite databse code
public class DatabaseStudent extends SQLiteOpenHelper {
public static final String databasename="Student";
public static final String Student_table="Student_table";
public static final String Student_name="Name";
public static final String Student_rollnumber="RollNumber";
public static final String Student_College="College";
public static final String Student_Branch="Branch";
public static final String Student_Year="Year";
public static final String Parent_name="ParentName";
public static final String Parent_address="ParentAddress";
public static final String Parent_Phone_number="Phone";
public static final String Student_Email_ID="Email";
public static final String Student_Password="Password";
public static final int versioncode=1;
public DatabaseStudent(Context context){
super(
context,
databasename,
null,
versioncode);
}
@Override
public void onCreate(SQLiteDatabase database) {
String student_query;
student_query="CREATE TABLE IF NOT EXISTS "+Student_table+"(Name TEXT,RollNumber NUMBER PRIMARY KEY,College TEXT,Branch TEXT,Year TEXT,ParentName TEXT,ParentAddress TEXT,Phone NUMBER, Email Text, Password TEXT)";
database.execSQL(student_query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String student_query;
student_query= "DROP TABLE IF EXISTS " + Student_table;
database.execSQL(student_query);
}
public boolean Student_Data(String name,String rollnumber,String college,String branch,String year,String parentname,String parentaddress,String phone,String email,String password){
SQLiteDatabase db1=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(Student_name,name);
cv.put(Student_rollnumber,rollnumber);
cv.put(Student_College,college);
cv.put(Student_Branch,branch);
cv.put(Student_Year,year);
cv.put(Parent_name,parentname);
cv.put(Parent_address,parentaddress);
cv.put(Parent_Phone_number,phone);
cv.put(Student_Email_ID,email);
cv.put(Student_Password,password);
long result=db1.insert(Student_table,null,cv);
if(result==-1)
return false;
else
return true;
}
public Cursor StudentData()
{
SQLiteDatabase db1=this.getWritableDatabase();
Cursor res = db1.rawQuery("select * from "+Student_table,null);
return res;
}
```
```
**java code**
public class StudentProfileActivity extends AppCompatActivity {
EditText editname,editroll,editcollege,editbranch,edityear,editparentname,editparentaddress,editphone,editemail,editpassword;
SQLiteDatabase sqLiteDatabase;
DatabaseStudent dbh;
Cursor res;
Student_Listadapter la;
Button btnupadte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_profile);
editname=findViewById(R.id.editname);
editroll=findViewById(R.id.editrollnumber);
editcollege=findViewById(R.id.editcollege);
editbranch=findViewById(R.id.editbranch);
edityear=findViewById(R.id.edityear);
editparentaddress=findViewById(R.id.editparentaddress);
editparentname=findViewById(R.id.editparentname);
editphone=findViewById(R.id.editphone);
editemail=findViewById(R.id.editemail);
editpassword=findViewById(R.id.editpassword);
btnupadte=findViewById(R.id.btnupdate);
dbh = new DatabaseStudent(getApplicationContext());
sqLiteDatabase=dbh.getReadableDatabase();
res=dbh.StudentData();
if(res.moveToFirst())
{
do{
String name,rollnumber,college,branch,year,parentname,parentaddress,phone,email,password;
name= res.getString(0);
rollnumber=res.getString(1);
college=res.getString(2);
branch=res.getString(3);
year=res.getString(4);
parentname=res.getString(5);
parentaddress=res.getString(6);
phone=res.getString(7);
email=res.getString(8);
password=res.getString(9);
Dataprovider_Student DPC= new Dataprovider_Student(name,rollnumber,college,branch,year,parentname,parentaddress,phone,email,password);
la.add(DPC);
}while(res.moveToNext());
}
btnupadte.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
```
```
**xml code**
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/studentlist"
android:padding="10dp"
tools:context=".StudentProfileActivity">
<EditText
android:id="@+id/editname"
android:hint="Name"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editrollnumber"
android:hint="Roll number"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editcollege"
android:hint="college"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editbranch"
android:hint="branch"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edityear"
android:hint="year"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editparentname"
android:hint="parentname"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editparentaddress"
android:hint="parentaddress"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editphone"
android:hint="phone"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editemail"
android:hint="email"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editpassword"
android:hint="password"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnupdate"
android:text="UPDATE"
android:textColor="#fff"
android:background="#009688"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
</details>
# 答案1
**得分**: 0
将从SQLite数据库检索到的文本设置到布局中的编辑文本视图,类似于以下代码:
在StudentProfileActivity.java中,在do-while循环内,在“la.add(DPC);”之后:
editname.setText(name);
editroll.setText(rollnumber);
editcollege.setText(college);
// 其他代码...
<details>
<summary>英文:</summary>
Set the retrieved text (from SQLite DB) to edit text views in layout, similar to below code
In StudentProfileActivity.java, within do-while loop, after line "la.add(DPC);"
editname.setText(name);
editroll.setText(rollnumber);
editcollege.setText(college);
.
.
.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论