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

  • Home
  • SEARCH
  • 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 7940935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:30:54+00:00 2026-06-03T23:30:54+00:00

I have a DatabaseHelper class and DatabaseAdapter class. When i try to select data

  • 0

I have a DatabaseHelper class and DatabaseAdapter class. When i try to select data from several activities, i get empty database. What am i doing wrong? I am reading or writing to dabase while calling database adapter and passing current activity as context.

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "MySampleDatabase";
    private static final int DATABASE_VERSION = 15;
    private final Context context;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        AssetManager manager = context.getResources().getAssets();
        InputStream is;
        try {
            is = manager.open("SQLStatements");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuffer sb = new StringBuffer();
            String line;
            while((line = reader.readLine()) != null){
                sb.append(line);
            }
            String[] queries = (new String(sb)).split(";");
            is.close();
            for(int i = 0; i < queries.length; i++){
                db.execSQL(queries[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS UserDrugs");
        db.execSQL("DROP TABLE IF EXISTS UserDrugsUsages");
        onCreate(db);
    }
}
public class DatabaseUserDrugsUsagesAdapter {
    public static final String KEY_USER_DRUG_ID = "UserDrugID";
    public static final String KEY_PACKAGE_ID = "PackageID";
    public static final String KEY_TIME_TO_USE = "TimeToUse";
    public static final String KEY_USED = "Used";
    public static final String KEY_PACKAGE_NAME = "PackageName";
    private static final String DATABASE_TABLE = "UserDrugsUsages";
    private Context context;
    private SQLiteDatabase database;
    private DatabaseHelper dbHelper;

    public DatabaseUserDrugsUsagesAdapter(Context context) {
        this.context = context;
    }

    public DatabaseUserDrugsUsagesAdapter open() throws SQLException {
        dbHelper = new DatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        dbHelper.close();
    }

    public void clearDatabaseTable(){
        database.execSQL("DELETE FROM " + DATABASE_TABLE);
    }

    public Cursor selectUserDrugUsagesByDate(String date){
        return database.query(DATABASE_TABLE, new String[] {
                KEY_USER_DRUG_ID,
                KEY_PACKAGE_ID,
                KEY_TIME_TO_USE,
                KEY_USED,
                KEY_PACKAGE_NAME}, "TimeToUse like '" + date + "%'", null, null, null, null);
    }

    public long writeUserDrugUsage(int userDrugId, int packageId, String timeToUse, boolean used, String packageName) {
        ContentValues initialValues = createContentValues(userDrugId, packageId, timeToUse, used, packageName);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    private ContentValues createContentValues(int userDrugId, int packageId, String timeToUse, boolean used, 
            String packageName) {

        ContentValues values = new ContentValues();
        values.put(KEY_USER_DRUG_ID, userDrugId);
        values.put(KEY_PACKAGE_ID, packageId);
        values.put(KEY_TIME_TO_USE, timeToUse);
        values.put(KEY_USED, used);
        values.put(KEY_PACKAGE_NAME, packageName);

        return values;
    }
}

EDIT:
There is the code where i call select function.

Cursor c = null;
String name = null;
DatabaseUserDrugsUsagesAdapter db = new DatabaseUserDrugsUsagesAdapter(context);
db.open();
c = db.selectDrugNameById(id);
db.close();

if(c != null && c.getCount() > 0){
    c.moveToFirst();
    name = c.getString(4);
}
c.close();
  • 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-06-03T23:30:56+00:00Added an answer on June 3, 2026 at 11:30 pm

    This is because you are you closed your database before manipulating the cursor.

    Actually Cursor is like a list/pointer created from a database resource.

    I changed your code and it will work just fine give it a try

    Cursor c = null;
    String name = null;
    DatabaseUserDrugsUsagesAdapter db = new DatabaseUserDrugsUsagesAdapter(context);
    db.open();
    c = db.selectDrugNameById(id);
    
    if(c != null && c.getCount() > 0){
         c.moveToFirst();
         name = c.getString(4);
            }
        c.close();
        db.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database helper class private static class DatabaseHelper extends SQLiteOpenHelper{ DatabaseHelper(Context context){
I have an AsyncTask which launches a DatabaseHelper class from DoinBackground which copies an
I have created a sub class (DatabaseHelper) of SQLiteOpenHelper Class.Its has a constructor and
I'm using an sqlite database in my app. I have this dbhelper class in
I'm trying to display data from my database in a ListFragment. To do this,
I have code like this: public class DatabaseHelper extends SQLiteOpenHelper { private static final
I have a database that I have created with the following handler: public class
I have a DatabaseHelper class which extends SQLiteOpenHelper, what is a good practice to
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I currently have a .csv file with several unlabeled columns of data, which to

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.