Reputation: 1495
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);
Upvotes: 0
Views: 2913
Reputation: 1495
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.
Upvotes: 0
Reputation: 1823
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:
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();
}
Upvotes: 2
Reputation: 109237
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'
Upvotes: 0