How to get a value from a database and display it in a text view in android

I am very sorry to ask for this simple doubt. I just started android and now studied database partitions in android.

I created a database with the name "books" and with the name of the table "title".

Then I drag the database into the assets section of the project.

Now I want to get the value from the database (any value from five columns) and display it in text form. Someone please help me how to do this.

Below is the code that I used to read the database.

import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DBAdapter { public static final String KEY_ROWID = "_id"; public static final String KEY_ISBN = "isbn"; public static final String KEY_TITLE = "title"; public static final String KEY_PUBLISHER = "publisher"; private static final String TAG = "DBAdapter"; private static final String DATABASE_NAME = "books"; private static final String DATABASE_TABLE = "titles"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "create table titles (_id integer primary key autoincrement, " + "isbn text not null, title text not null, " + "publisher text not null);"; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS titles"); onCreate(db); } } //---opens the database--- public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } //---closes the database--- public void close() { DBHelper.close(); } //---insert a title into the database--- public long insertTitle(String isbn, String title, String publisher) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_ISBN, isbn); initialValues.put(KEY_TITLE, title); initialValues.put(KEY_PUBLISHER, publisher); return db.insert(DATABASE_TABLE, null, initialValues); } //---deletes a particular title--- public boolean deleteTitle(long rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; } //---retrieves all the titles--- public Cursor getAllTitles() { return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, null, null, null, null, null); } //---retrieves a particular title--- public Cursor getTitle(long rowId) throws SQLException { Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER }, KEY_ROWID + "=" + rowId, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } //---updates a title--- public boolean updateTitle(long rowId, String isbn, String title, String publisher) { ContentValues args = new ContentValues(); args.put(KEY_ISBN, isbn); args.put(KEY_TITLE, title); args.put(KEY_PUBLISHER, publisher); return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; } } 

main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 

DatabaseActivity.java

 import android.app.Activity; import android.os.Bundle; public class DatabaseActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

enter image description here

Hope for your help. Thanks in advance.

+4
source share
2 answers

Create a class and extend it with contentProvider and use it as follows

 public class XyzDB extends ContentProvider { static Context context; private SQLiteOpenHelper mOpenHelper; public XyzDB() {} public XyzDB(Context context) { XyzDB.context = context; mOpenHelper = new DatabaseHelper(context); mOpenHelper.getWritableDatabase(); } 

and also create a class in XyzDB, like this

 private static class DatabaseHelper extends SQLiteOpenHelper { //create table here } 

and then enter the following method

You should get the data from the cursor in the database helper class. You must create such a method and return the value of the cursor.

 public Cursor fetchData() { SQLiteDatabase mDB = mOpenHelper.getWritableDatabase(); Cursor mCursor; mCursor = mDB.rawQuery("Select * from table, null); return mCursor; } 

and create a class object of the auxiliary database object in your activity and call this method and retrieve the data from the cursor, as shown

 Cursor c = jDB.fetchDataByID(id); if(c.moveToFirst()) { do { id = c.getInt(0); }while(c.moveToNext()); } c.close(); 

and set id or any value in the text view.

and follow this link

http://www.vogella.de/articles/AndroidSQLite/article.html

+3
source

I will give an example with this, and you can easily understand how we can display data from a database in text form. Below is the code in which testdata is the name of the database in which there is a user table in which there are 2 columns. I am showing the data of these 2 columns by 2 diff. TextViews

 public class high_score extends Activity{ SQLiteDatabase mydatabase=null; String Data=""; String Data1=""; String TableName="users"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.high_score); RelativeLayout main=(RelativeLayout)findViewById(R.id.main); try { mydatabase=this.openOrCreateDatabase("testdata", MODE_PRIVATE, null); Cursor c=mydatabase.rawQuery("SELECT DISTINCT name,score FROM "+TableName+" , null); int column1=c.getColumnIndex("name"); int column2=c.getColumnIndex("max_score"); c.moveToFirst(); if(c!=null) { do { String Name=c.getString(column1); int Score=c.getInt(column2); Data=Data+Name+"\n"; Data1=Data1+Score+"\n"; System.out.println("name"+Name+" score"+Score); }while(c.moveToNext()); TextView tv=(TextView)findViewById(R.id.tv); TextView points=(TextView)findViewById(R.id.points); tv.setText(Data); points.setText(Data1); setContentView(main); } /*TextView tv=(TextView)findViewById(R.id.tv); tv.setText(Data); setContentView(tv);*/ }catch(Exception ex) { Log.e("ERROR","ERROR" ,ex); } finally { if(mydatabase!=null) mydatabase.close(); } 

you can easily access such data

+3
source

Source: https://habr.com/ru/post/1398542/


All Articles