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 8251009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:05:57+00:00 2026-06-08T00:05:57+00:00

I want to use a database in my application, that was created in advance

  • 0

I want to use a database in my application, that was created in advance and then use a cursor loader to present the data from the db.
First I copy the database from the assets folder according to this tutorial [http://goo.gl/1XS84] and then I want to use CursorLoader with CursorAdapter.

The CursorLoader though needs some query Uri of the database and I don´t know what URI I should specify.
Any help?

  • 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-08T00:05:59+00:00Added an answer on June 8, 2026 at 12:05 am

    Here is an excerpt how you can use your database from assets in a content provider.

    public class MyProvider extends ContentProvider {
    
        private SQLiteOpenHelper mOpenHelper;
        ...
    
        private static class DatabaseHelper extends SQLiteOpenHelper {
    
            private static final String DATABASE_NAME = "database.db";
            private static final int DATABASE_VERSION = 1;
            private static final String SP_KEY_DB_VER = "db_ver";
            private final Context mContext;
                ...
    
            public DatabaseHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
                mContext = context;
                initialize();
            }
    
            /**
             * Initializes database. Creates database if doesn't exist.
             */
            private void initialize() {
                if (databaseExists()) {
                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(mContext);
                    int dbVersion = prefs.getInt(SP_KEY_DB_VER, 1);
                    if (DATABASE_VERSION != dbVersion) {
                        File dbFile = mContext.getDatabasePath(DATABASE_NAME);
                        if (!dbFile.delete()) {
                            Log.w(TAG, "Unable to update database");
                        }
                    }
                }
                if (!databaseExists()) {
                    createDatabase();
                }
            }
    
            /**
             * Returns true if database file exists, false otherwise.
             */
            private boolean databaseExists() {
                File dbFile = mContext.getDatabasePath(DATABASE_NAME);
                return dbFile.exists();
            }
    
            /**
             * Creates database by copying it from assets directory.
             */
            private void createDatabase() {
                String parentPath = mContext.getDatabasePath(DATABASE_NAME).getParent();
                String path = mContext.getDatabasePath(DATABASE_NAME).getPath();
    
                File file = new File(parentPath);
                if (!file.exists()) {
                    if (!file.mkdir()) {
                        Log.w(TAG, "Unable to create database directory");
                        return;
                    }
                }
    
                InputStream is = null;
                OutputStream os = null;
                try {
                    is = mContext.getAssets().open(DATABASE_NAME);
                    os = new FileOutputStream(path);
    
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = is.read(buffer)) > 0) {
                        os.write(buffer, 0, length);
                    }
                    os.flush();
                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(mContext);
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putInt(SP_KEY_DB_VER, DATABASE_VERSION);
                    editor.commit();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (os != null) {
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    
            @Override
            public void onCreate(SQLiteDatabase db) {
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion,
                    int newVersion) {
            }
        }
        ...
    

    Note: I would recommend doing I/O operations in a separate thread.

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

Sidebar

Related Questions

First, I don't want to use a database or a local file. Is it
I want to use jQuery to pull information from my database when I click
I want to use string similarity functions to find corrupted data in my database.
I created a new mysql database and i want to use foreign keys with
I am creating an application that has to interact with server data and then
I am creating desktop application in winform that will use Sqlite Database. So I
I use auto generated Entity-Framework Data Model for a Database that has 100+ tables.
I want to have an application that has a permanent database file(on a hard
I have created a C# application that I want to split into server and
I have created a vb.net application that uses a SQL Server database at a

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.