Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:09:31+00:00 2026-05-30T16:09:31+00:00

Im very sorry for asking for this simple doubt.Im just started to android and

  • 0

Im very sorry for asking for this simple doubt.Im just started to android and now studying the database sections in android.

I created a database with name “books” and with table name “titles”.

Then i drag and drop the database into the assets section in project.

Now i want to fetch a value from the database(any value from the five columns) and display in the text view.Can anyone please help me how to do this.

Below is the code 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

Hoping for your help.Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:09:32+00:00Added an answer on May 30, 2026 at 4:09 pm

    Create class and extends it with contentProvider and use it like this

    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 within XyzDB like this

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

    and then put following method

    You have to fetch data from cursor in database helper class. You have to create method like this and return cursor value.

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

    and create Database helper class object in your activity and call this method and fetch data from cursor like this

    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 text view.

    and follow this link

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for asking very simple question.I'm new to coding. Input txt file 5,3 001
First of I'm very sorry but this questions is not so so specific. All
This is a question corresponds to my uni assignment so I am very sorry
I'm sorry for this very newbish question, I'm not much given into web development.
Sorry if this question is very easy, but I can't find the answer anywhere.
Sorry the title is not very clear. This is a follow up to my
Sorry this is not a very well defined question, I am thinking about an
Sorry, I know this is a very lame question to ask and not of
I might be asking a very basic question and I am sorry for that.
I am very sorry if my question doesn't make sense as this is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.