0

I have database called test.sqlite and i have copied the database file in to data/data/packagename/databases folder using DDMS. When i am trying read the database i am getting the error as table does not exist What is the solution to access the table.

Below is my code to access database.

SQLiteDatabase myDB = this.openOrCreateDatabase("test.sqlite",
              SQLiteDatabase.OPEN_READWRITE, null);
Cursor c = myDB.rawQuery("select a, b from abc", null); 
6
  • Where did you copy it? Exact path please. Commented Jan 2, 2012 at 11:58
  • is that location data/data/your.package.name/databases ? Commented Jan 2, 2012 at 11:59
  • yes database path is data/data/package/databases Commented Jan 2, 2012 at 12:03
  • Try to change the owner of your db to application's uid. Commented Jan 2, 2012 at 12:04
  • @Yury: I am new android can you explain me with the example to change the owner Commented Jan 2, 2012 at 12:05

3 Answers 3

2

Why you want to put the database file direct into the data/data folder? For testing purposes? Don't make sense to me, I would choose one of the options below:

  • Load data from the server and insert it into the database.
  • Put the database file in the assets folder, and then copy the bytes to create the app database (you can't move the database file).

Example (as requested):

First you need to put your database file inside the assets/database folder. If your database is bigger than 1mb you need to split it. (i.e. database1, database2, database3...)

You probably have a class to do all database tasks, here I have a class called DatabaseHelper (extends SQLiteOpenHelper). Don't forget to set you database path and name.

private static String DB_PATH = "/data/data/com.example/databases/";
private static String DB_NAME = "mydb.sqlite";

If you had to split your database, declare the pieces into a variable too.

private static String[] sFiles = {'database1','database2','database3'};

You can create/open your database quickly using the getReadableDatabase() method.

Then, create an implementation like this:

private void copyDatabase() throws IOException {
    // Path to the empty database.
    String outFilename = DB_PATH + DB_NAME;
    // Open the empty database as the output stream.
    OutputStream outputDatabase = new FileOutputStream(outFilename);
    // Transfer bytes from the input file to the output file.
    byte[] buffer = new byte[1024];

    for (int i = 0; i < sFiles.length; ++i) {
        // Open the local database as the input stream.
        InputStream is = mContext.getAssets().open("database/" + sFiles[i]);

        int length;
        while ((length = is.read(buffer)) > 0) {
            outputDatabase.write(buffer, 0, length);
        }
        // Closing stream.
        is.close();
    }

    // Closing the streams.
    outputDatabase.flush();
    outputDatabase.close();
    // Closing database.
    close();        
}
Sign up to request clarification or add additional context in comments.

4 Comments

i have so much data in the server side, so the sqlite will take so much time to load the data, so i am using existing database
Ok, so this is for test only, right? In the final version you will download the data or you need to deploy your app with data built in?
no actually the sqlite is build in iphone application, so i am using same sqlite in android so it will take less time right.
I had the same problem in the past. I had to create an Android app from a iOS version. The database from the iOS app had 16mb. The solution that I found at that time was to put the database file in the raw folder and then read the file and write it in the database directory from the app, since is not possible to copy a file from the apk folder to the app dir. So, you'll probably need to do that too to release your app with the data. :) Also, I had to split the database file in 16 pieces, because you can't work with files bigger than 1mb... ;)
0

Copy your sqlite database file in data/data/<package_name>/databases directory.

Then be sure you are opening the copied sqlite database file..

Also to check whether you are opening a correct database and which table in this, look at this query,

SELECT * FROM sqlite_master where type='table' and name='my_table_name'

1 Comment

Yes i did like this but no use
0

Thanks for the replies finally it is working. i did a mistake actually i copied the database file in data/data/packagename so it is caused the error.

What i did was again copied the database in data/data/packagename/databases folder.

SQLiteDatabase myDB = this.openOrCreateDatabase("test.sqlite",
              SQLiteDatabase.OPEN_READWRITE, null);
Cursor c = myDB.rawQuery("select a, b from abc", null); 

This is working fine for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.