Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access an existing sqlite database in Android?

Tags:

android

sqlite

So far we have developed apps in android that create database on runtime. We like to know how can we access a pre-built or existing database/sqlite file in our android app? Please provide detail

like image 835
Muhammad Maqsoodur Rehman Avatar asked Dec 23 '09 10:12

Muhammad Maqsoodur Rehman


People also ask

How do I connect to an existing SQLite database?

Use the connect() method To establish a connection to SQLite, you need to pass the database name you want to connect. If you specify the database file name that already presents on the disk, it will connect to it. But if your specified SQLite database file doesn't exist, SQLite creates a new database for you.

Where are SQLite databases stored android?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.


2 Answers

Take a look at the documentation for android.database.sqlite.SQLiteDatabase.

In particular, there's an openDatabase() command that will access a database given a file path.

SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0); 

Just specify the path to your database as the path variable, and it should work.

like image 103
Trevor Johns Avatar answered Sep 30 '22 23:09

Trevor Johns


I have followed the same road and found two issues:

  1. Include the _id field on any table you create and make sure it is part of the columns to return when you query any table.
  2. You may run into an error like Failed to open the database. closing it.. This is because we are copying the database from an existing file instead of creating tables on top of a db created by the sqlite android API and the table android_metadata may not exist. For creating it just run the following query on your db:

    CREATE TABLE android_metadata (locale TEXT);

    And add the corresponding locale, for example en_US

like image 28
Andres Felipe Avatar answered Sep 30 '22 23:09

Andres Felipe