如何从 SQLite 数据库中获取数据并在 EditText 中显示

Posted

技术标签:

【中文标题】如何从 SQLite 数据库中获取数据并在 EditText 中显示【英文标题】:How to get data from SQLite database and display it in EditText 【发布时间】:2020-04-21 19:26:33 【问题描述】:

我需要帮助才能从 SQLite 获取数据并在 EditText 中显示我为 getdata 编写的代码是正确的,如何在 EditText 中显示部分请帮助我。我需要将数据从数据库显示到 EditText 的确切代码

提前致谢

```
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_
    android:layout_
    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_
        android:layout_ />
    <EditText
        android:id="@+id/editrollnumber"
        android:hint="Roll number"
        android:ems="10"

        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/editcollege"
        android:hint="college"
        android:ems="10"

        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/editbranch"
        android:hint="branch"
        android:ems="10"

        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/edityear"
        android:hint="year"
        android:ems="10"

        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/editparentname"
        android:hint="parentname"
        android:ems="10"
        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/editparentaddress"
        android:hint="parentaddress"
        android:ems="10"
        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/editphone"
        android:hint="phone"
        android:ems="10"
        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/editemail"
        android:hint="email"
        android:ems="10"
        android:layout_
        android:layout_ />
    <EditText
        android:id="@+id/editpassword"
        android:hint="password"
        android:ems="10"
        android:layout_
        android:layout_ />

    <Button
        android:id="@+id/btnupdate"
        android:text="UPDATE"
        android:textColor="#fff"
        android:background="#009688"
        android:layout_
        android:layout_ />


```

【问题讨论】:

【参考方案1】:

设置检索到的文本(来自 SQLite DB)以编辑布局中的文本视图,类似于下面的代码

在 StudentProfileActivity.java 中,在 do-while 循环中,在“la.add(DPC);”行之后

editname.setText(name);
editroll.setText(rollnumber);
editcollege.setText(college);
.
.
.

【讨论】:

请始终将您的答案放在上下文中,而不仅仅是粘贴代码。有关详细信息,请参阅here。

以上是关于如何从 SQLite 数据库中获取数据并在 EditText 中显示的主要内容,如果未能解决你的问题,请参考以下文章

如何从我的 sqlite 数据库中获取数据

如何从 Javascript 中的 SQLite 数据库中获取表数据?

如何从mysql中获取数据并存储在android的Sqlite数据库中

如何从 Room 数据库中获取 SQLite 数据库实例引用?

如何根据日期列中的月份从 SQLite 中获取数据

如何使用 SQLite 从数据库表中获取一个字段?