Android Studio how to retrieve data from Sqlite database and display it into textview

0 votes

I built a SQLite Database and added the data to it in my app. And right now I want to get data out of it, but I only want to insert one data, get it, get it back, and then display it in a TextView.

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));
        }  
    }
}

Can someone please help me with this?

Sep 2, 2022 in Database by Kithuzzz
• 38,000 points
• 2,844 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Here is an illustration of how I did it. I'll save, retrieve, edit, and delete a student's name and age in this example.

First, create a class:

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);
    }
}

Then create a 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);
    }   
}

TO ADD:

In this example, I use the text from EditText, and after clicking the button, I determine whether or not EditText is empty. If it's not empty and the student isn't already in the database, I enter the student's name and age. I present a toast informing the user of the situation:

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();
            }
        }
    }  
});

I hope this helps you.

answered Sep 3, 2022 by narikkadan
• 86,360 points

edited Mar 5, 2025

Related Questions In Database

0 votes
2 answers

How to send data to my database from html and css Contact Us Form?

Hello @Sign, It is simple to create contact ...READ MORE

answered Aug 4, 2020 in Database by Niroj
• 82,800 points
• 38,448 views
0 votes
0 answers

Displaying ALL data from sqlite database into listview in tabbed activity

I've been struggling with this problem for ...READ MORE

Aug 20, 2022 in Database by Kithuzzz
• 38,000 points
• 4,892 views
0 votes
0 answers

Display data from SQL database into php/ html table.

I want to display one of my ...READ MORE

Aug 27, 2022 in Database by Kithuzzz
• 38,000 points
• 2,063 views
+2 votes
1 answer

How to export data from MySql to a CSV file?

If you are using MySql workbench then ...READ MORE

answered Jan 4, 2019 in Database by Priyaj
• 58,020 points
• 3,019 views
0 votes
1 answer

How to load data of .csv file in MySQL Database Table?

At first, put the dataset in the ...READ MORE

answered Jul 5, 2019 in Database by Reshma
• 2,691 views
+1 vote
1 answer

How to migrate a PostgreSQL database into a SQLServer one?

Hello @kartik, The easier way to accomplish this. First ...READ MORE

answered May 13, 2020 in Database by Niroj
• 82,800 points
• 2,511 views
0 votes
1 answer

How to retrieve data from sqlite database in android and display it in TextView

When a button is clicked, the database ...READ MORE

answered Aug 11, 2022 in Database by narikkadan
• 86,360 points

edited Mar 5, 2025 • 6,894 views
0 votes
0 answers

SQLite in Android How to update a specific row

For a time now, I've been attempting ...READ MORE

Aug 11, 2022 in Database by Kithuzzz
• 38,000 points
• 3,063 views
0 votes
0 answers

Create SQLite database in android

In my project, I want to construct ...READ MORE

Sep 4, 2022 in Database by Kithuzzz
• 38,000 points
• 1,294 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP