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

huangapple go评论73阅读模式
英文:

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=&quot;Student&quot;;
    
        public static final String Student_table=&quot;Student_table&quot;;
    
        public static final String Student_name=&quot;Name&quot;;
        public static final String Student_rollnumber=&quot;RollNumber&quot;;
        public static final String Student_College=&quot;College&quot;;
        public static final String Student_Branch=&quot;Branch&quot;;
        public static final String Student_Year=&quot;Year&quot;;
        public static final String Parent_name=&quot;ParentName&quot;;
        public static final String Parent_address=&quot;ParentAddress&quot;;
        public static final String Parent_Phone_number=&quot;Phone&quot;;
        public static final String Student_Email_ID=&quot;Email&quot;;
        public static final String Student_Password=&quot;Password&quot;;
        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=&quot;CREATE TABLE IF NOT EXISTS &quot;+Student_table+&quot;(Name TEXT,RollNumber NUMBER PRIMARY KEY,College TEXT,Branch TEXT,Year TEXT,ParentName TEXT,ParentAddress TEXT,Phone NUMBER, Email Text, Password TEXT)&quot;;
            database.execSQL(student_query);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    
            String student_query;
            student_query= &quot;DROP TABLE IF EXISTS &quot; + 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(&quot;select * from &quot;+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**
    
    &lt;LinearLayout
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:orientation=&quot;vertical&quot;
        android:gravity=&quot;center&quot;
        android:background=&quot;@drawable/studentlist&quot;
        android:padding=&quot;10dp&quot;
        tools:context=&quot;.StudentProfileActivity&quot;&gt;
    
    
        &lt;EditText
            android:id=&quot;@+id/editname&quot;
            android:hint=&quot;Name&quot;
            android:ems=&quot;10&quot;
    
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editrollnumber&quot;
            android:hint=&quot;Roll number&quot;
            android:ems=&quot;10&quot;
    
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editcollege&quot;
            android:hint=&quot;college&quot;
            android:ems=&quot;10&quot;
    
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editbranch&quot;
            android:hint=&quot;branch&quot;
            android:ems=&quot;10&quot;
    
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/edityear&quot;
            android:hint=&quot;year&quot;
            android:ems=&quot;10&quot;
    
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editparentname&quot;
            android:hint=&quot;parentname&quot;
            android:ems=&quot;10&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editparentaddress&quot;
            android:hint=&quot;parentaddress&quot;
            android:ems=&quot;10&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editphone&quot;
            android:hint=&quot;phone&quot;
            android:ems=&quot;10&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editemail&quot;
            android:hint=&quot;email&quot;
            android:ems=&quot;10&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
        &lt;EditText
            android:id=&quot;@+id/editpassword&quot;
            android:hint=&quot;password&quot;
            android:ems=&quot;10&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
    
        &lt;Button
            android:id=&quot;@+id/btnupdate&quot;
            android:text=&quot;UPDATE&quot;
            android:textColor=&quot;#fff&quot;
            android:background=&quot;#009688&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
    
    
    ```

</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 &quot;la.add(DPC);&quot;
editname.setText(name);
editroll.setText(rollnumber);
editcollege.setText(college);
.
.
.
</details>

huangapple
  • 本文由 发表于 2020年1月3日 16:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定