How to can i use the SQLITE Database folder from databases folder in Android?
-
21-12-2019 - |
سؤال
I am trying to write a code in Android to use my existing sqlite database file from databases folder. I copied the database from Asset folder to databases folder. Now i want to use the sqlite file from my Provider but i am not finding any technique to do it.
My Code :
MainActivity.java
package com.example.testtable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentValues updateStatus = new ContentValues();
updateStatus.put(TestProvider.Name,"Android Code");
this.getApplicationContext().getContentResolver().update(TestProvider.CONTENT_URI, updateStatus, "Id=? AND AgeId=? AND PhoneNo=?", new String[]{"1","25","9807456783"});
} }
TestProvider.java
package com.example.testtable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;
public class TestProvider extends ContentProvider
{
private SQLiteDatabase database;
static final String PROVIDER_NAME = "com.example.testtable.provider.TestProvider";
static final String TestTable = "MyDetails";
static final String URL = "content://" + PROVIDER_NAME +"/" +TestTable;
static final Uri CONTENT_URI = Uri.parse(URL);
static final String TestId = "TestId";
static final String Name = "Name";
static final String ID = "Id";
static final String AgeId = "AgeId";
static final String PhoneNo = "PhoneNo";
static final int DATABASE_VERSION = 1;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
private Context cont;
private SQLiteDatabase database;
public static String DB_PATH;
public static final String DB_NAME = "testdb.sqlite";
DatabaseHelper(Context context)
{
super(context,null,null,DATABASE_VERSION);
this.cont = context;
String packageName = context.getPackageName();
DB_PATH = "/data/data/" + packageName + "/databases/";
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
InputStream myInput = cont.getAssets().open(DB_NAME);
String outFileName = "/data/data/testtable/databases/" + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
catch(IOException e)
{
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
//
}
}
@Override
public boolean onCreate()
{
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return (database==null)? false:true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)
{
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(TestTable);
Cursor c = qb.query(database, projection, selection, selectionArgs,
null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs)
{
int count = 0;
switch(uriMatcher.match(uri))
{
case 1:
count = database.update(TestTable, values, selection, selectionArgs);
break;
case 2:
count = database.update(TestTable, values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URL: "+uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
These are all my above codes. Please let me know how can i use the copied DB file from database folder to my SQL Queries.
Please suggest me some solution.
المحلول
I see two problems with your code:
- (1) Your databasehelper must know the name of the database to use
- (2) your code make assumptions where the database file is located
/data/data/testtable/databases/
This might varay for different android-version/sdcard, .....
// TestProvider.java
public class TestProvider extends ContentProvider {
public static final String DB_NAME = "testdb.sqlite";
// (2) let android give you the path to the local database
public File getLocalDatabaseFile(Context context) {
return context.getDatabasePath(DB_NAME);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
// (1) parameter#2: databasehelper must know the name of the database to use
super(context,DB_NAME,null,DATABASE_VERSION);
...
}
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
InputStream myInput = cont.getAssets().open(DB_NAME);
// (2) let android give you the path to the local database
OutputStream myOutput = new FileOutputStream(
getLocalDatabaseFile(this.context));
نصائح أخرى
AFAIK, that super() call within DatabaseHelper must carry the filename (and you have null). No files will be created until you try an operation, have you tried to just supply the filename? As a fallback, have you considered using the external file system?
You can use android-sqlite-asset-helper to copy the database from the assets folder to the phone.
Extend the SQLiteAssetHelper
instead of SQLiteOpenHelper
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "northwind.db";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
and use an instance of MyDatabase
in the content provider.
The database will be copied to the phone the first time getReadableDatabase
or getWritableDatabase
is called.