1

It is possible to use an already created database sqlite in android? I already created database in sqlite in mozilla ad-ons. How should I use it in my android application? Anyone can help me??

  • Refer to this site http://zaman91.wordpress.com/2010/09/22/android-how-to-use-own-sqlite-database/ – Cjames May 15 '13 at 01:46

3 Answers3

2

First, to use a database, in general, in android, you should extend the SQLiteOpenHelper class. This class is the one responsible for creating your database (and upgrading) when needed from a sql script you provide in your implementation.

So the trick is, you need to override the behavior of the SQLiteOpenHelper to copy your database file from the assets folder instead of create your database.

in this blog post, i explain in details the process of overriding this behavior. but here is the final code.

use the Repository class as you would use SQLiteOpenHelper normally.

public class Repository extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DATABASE_NAME = "data.sqlite";
private static File DATABASE_FILE;
// This is an indicator if we need to copy the
// database file.
private boolean mInvalidDatabaseFile = false;
private boolean mIsUpgraded = false;
private Context mContext;
/**
* number of users of the database connection.
* */
private int mOpenConnections = 0;
private static Repository mInstance;

synchronized static public Repository getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new Repository(context.getApplicationContext());
    }
    return mInstance;
}
private Repository(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
    this.mContext = context;
    SQLiteDatabase db = null;
    try {
        db = getReadableDatabase();
        if (db != null) {
            db.close();
        }
        DATABASE_FILE = context.getDatabasePath(DATABASE_NAME);
        if (mInvalidDatabaseFile) {
            copyDatabase();
        }
        if (mIsUpgraded) {
            doUpgrade();
        }
    } catch (SQLiteException e) {
    } finally {
        if (db != null && db.isOpen()) {
            db.close();
        }
    }
}
@Override
public void onCreate(SQLiteDatabase db) {
    mInvalidDatabaseFile = true;
}
@Override
public void onUpgrade(SQLiteDatabase database,
                      int old_version, int new_version) {
    mInvalidDatabaseFile = true;
    mIsUpgraded = true;
}
/**
* called if a database upgrade is needed
*/
private void doUpgrade() {
    // implement the database upgrade here.
}
@Override
public synchronized void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    // increment the number of users of the database connection.
    mOpenConnections++;
    if (!db.isReadOnly()) {
        // Enable foreign key constraints
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}
/**
* implementation to avoid closing the database connection while it is in
* use by others.
*/
@Override
public synchronized void close() {
    mOpenConnections--;
    if (mOpenConnections == 0) {
        super.close();
    }
}
private void copyDatabase() {
    AssetManager assetManager = mContext.getResources().getAssets();
    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(DATABASE_NAME);
        out = new FileOutputStream(DATABASE_FILE);
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    } catch (IOException e) {
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {}
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {}
        }
    }
    setDatabaseVersion();
    mInvalidDatabaseFile = false;
}
private void setDatabaseVersion() {
    SQLiteDatabase db = null;
    try {
        db = SQLiteDatabase.openDatabase(DATABASE_FILE.getAbsolutePath(), null,
                                         SQLiteDatabase.OPEN_READWRITE);
        db.execSQL("PRAGMA user_version = " + VERSION);
    } catch (SQLiteException e ) {
    } finally {
        if (db != null && db.isOpen()) {
            db.close();
        }
    }
}
}
kdehairy
  • 2,630
  • 22
  • 27
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Cairnarvon May 15 '13 at 11:51
  • you are right @Cairnarvon, i modified my answer. but there is lots of details that don't fit here. – kdehairy May 15 '13 at 18:28
  • @user714965 it is an answer to the question, i was just too lazy :) .. i modified the answer – kdehairy May 15 '13 at 18:44
1

All you need to do is put the sqlite database in your assets folder, then when your app starts the first time, copy the database over to the SDCard.

Here is a great description of how to do this.

Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
0

Android uses internal databases for SQLite. If you want to use an external SQLite database (or any other external database) you're going to need to use something like an HHTP proxy. Here's a link that provides more info: https://stackoverflow.com/a/4124829/1852466

Community
  • 1
  • 1
Don
  • 506
  • 2
  • 8
  • 23
  • so if i'm going to push a sqlite database in my android it will be more complicated?? – Jess Mark Panadero May 15 '13 at 01:46
  • An external SQLite database, yes. If you just create a SQLite database on the phone, than it's pretty straightforward. You could also do what HalR mentioned above if you don't need to access up-to-date information from it from outside of the phone. – Don May 15 '13 at 01:55