java–android studio从Sqlite数据库中检索数据并将其显示到textview中

java–android studio从Sqlite数据库中检索数据并将其显示到textview中,第1张

概述嗨,大家好,我需要你的帮助.我在我的应用程序中创建SQLite数据库,然后将数据插入其中.现在我想从中检索数据,但我只想插入一个数据并检索它然后将其显示到TextView中.请帮助我们这是我第一次使用SQLite数据库.publicclassDb_sqlitextendsSQLiteOpenHelper{StringTABLE

嗨,大家好,我需要你的帮助.我在我的应用程序中创建sqlite数据库,然后将数据插入其中.现在我想从中检索数据,但我只想插入一个数据并检索它然后将其显示到TextVIEw中.

请帮助我们这是我第一次使用sqlite数据库.

public class Db_sqlit extends sqliteOpenHelper{    String table_name = "Ballstable";    public final static String name = "db_data";    public Db_sqlit(Context context) {        super(context, name, null, 1);    }    @OverrIDe    public voID onCreate(sqliteDatabase db) {        db.execsql("create table "+table_name+" (ID INTEGER PRIMARY KEY autoINCREMENT, ball TEXT)");    }    @OverrIDe    public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {        db.execsql("DROP table IF EXISTS "+table_name);        onCreate(db);    }    public boolean insertData(String balls){      sqliteDatabase db = this.getWritableDatabase();      ContentValues contentValues = new ContentValues();      contentValues.put("ball",balls);      long result = db.insert(table_name,null,contentValues);      if(result == -1){          return false;      }      else          return true;    }    public voID List_balls(TextVIEw textVIEw) {        Cursor res = this.getReadableDatabase().rawquery("select ball from "+table_name+"",null);        textVIEw.setText("");        while (res.movetoNext()){            textVIEw.append(res.getString(1));        }      }}

解决方法:

这是我如何实现这一目标的一个例子.

在这个例子中,我将存储,检索,更新和删除学生姓名和年龄.

首先创建一个类,我打电话给我

DBManager.java

public class DBManager {    private Context context;    private sqliteDatabase database;    private sqliteHelper dbHelper;    public DBManager(Context c) {        this.context = c;    }    public DBManager open() throws sqlException {        this.dbHelper = new sqliteHelper(this.context);        this.database = this.dbHelper.getWritableDatabase();        return this;    }    public voID close() {        this.dbHelper.close();    }    public voID insert(String name, String desc) {        ContentValues contentValue = new ContentValues();        contentValue.put(sqliteHelper.name, name);        contentValue.put(sqliteHelper.AGE, desc);        this.database.insert(sqliteHelper.table_name_STUDENT, null, contentValue);    }    public Cursor fetch() {        Cursor cursor = this.database.query(sqliteHelper.table_name_STUDENT, new String[]{sqliteHelper._ID, sqliteHelper.name, sqliteHelper.AGE}, null, null, null, null, null);        if (cursor != null) {            cursor.movetoFirst();        }        return cursor;    }    public int update(long _ID, String name, String desc) {        ContentValues contentValues = new ContentValues();        contentValues.put(sqliteHelper.name, name);        contentValues.put(sqliteHelper.AGE, desc);        return this.database.update(sqliteHelper.table_name_STUDENT, contentValues, "_ID = " + _ID, null);    }    public voID delete(long _ID) {        this.database.delete(sqliteHelper.table_name_STUDENT, "_ID=" + _ID, null);    }}

然后创建一个名为我的sqliteOpenHelper

sqliteHelper.java

public class sqliteHelper extends sqliteOpenHelper {    public static final String AGE = "age";    private static final String CREATE_table_STUDENT = " create table STUDENTS ( _ID INTEGER PRIMARY KEY autoINCREMENT, name TEXT NOT NulL , age TEXT );";    private static final String DB_name = "STUDENTS.DB";    private static final int DB_VERSION = 1;    public static final String name = "name";    public static final String table_name_STUDENT = "STUDENTS";    public static final String _ID = "_ID";    public sqliteHelper(Context context) {        super(context, DB_name, null, 1);    }    public voID onCreate(sqliteDatabase db) {        db.execsql(CREATE_table_STUDENT);    }    public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {        db.execsql("DROP table IF EXISTS STUDENTS");        onCreate(db);    }   }

加上:

在这个例子中,我从EditText中获取文本,当单击该按钮时,我检查EditText是否为空.如果它不是空的并且学生尚不存在,我将学生姓名和年龄插入数据库.我显示Toast,让用户知道状态:

btnAdd.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw vIEw) {        if (edtname.getText().toString().trim().length() == 0) {            Toast.makeText(getApplicationContext(), "Please provIDe your students name", Toast.LENGTH_SHORT).show();        } else{            try {                if (edtAge.getText().toString().trim().length() != 0) {                    String name = edtname.getText().toString().trim();                    String age = edtAge.getText().toString().trim();                    String query = "Select * From STUDENTS where name = '"+name+"'";                    if(dbManager.fetch().getCount()>0){                        Toast.makeText(getApplicationContext(), "Already Exist!", Toast.LENGTH_SHORT).show();                    }else{                        dbManager.insert(name, age);                        Toast.makeText(getApplicationContext(), "Added successfully!", Toast.LENGTH_SHORT).show();                                               }                } else {                    Toast.makeText(getApplicationContext(), "please provIDe student age!", Toast.LENGTH_SHORT).show();                                           }            } catch (Exception e) {                e.printstacktrace();            }        }    }  });

更新:
在这里,我在EditText中使用Text,并在单击按钮时更新学生.您还可以将以下内容放在try / catch中以确保它已成功更新.

btnupdate.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw vIEw) {        String name = nameText.getText().toString();        String age = ageText.getText().toString();        dbManager.update(_ID, name, age);        Toast.makeText(getApplicationContext(), "Updated successfully!", Toast.LENGTH_SHORT).show();    }});

删除:

dbManager.delete(_ID);Toast.makeText(getApplicationContext(), "Deleted successfully!", Toast.LENGTH_SHORT).show();

要得到:

在这里,我得到学生的名字并将其显示在TextVIEw中

DBManager dbManager = new DBManager(getActivity());dbManager.open();Cursor cursor = dbManager.fetch();cursor.movetoFirst();final TextVIEw studentname = (TextVIEw) getActivity().findVIEwByID(R.ID.nameOfStudent);studentname.settext(cursor.getString(0));
总结

以上是内存溢出为你收集整理的java – android studio从Sqlite数据库中检索数据并将其显示到textview中全部内容,希望文章能够帮你解决java – android studio从Sqlite数据库中检索数据并将其显示到textview中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1101141.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存