如何在android中使用现有数据库?

时间:2013-05-15 01:25:15

标签: android android-sqlite

可以在android中使用已创建的数据库sqlite吗?我已经在mozilla ad-ons中使用sqlite创建了数据库。我应该如何在我的Android应用程序中使用它?任何人都可以帮助我??

3 个答案:

答案 0 :(得分:2)

首先,要使用数据库,一般来说,在android中,你应该扩展SQLiteOpenHelper类。这个类负责在您实现时提供的sql脚本中根据需要创建数据库(和升级)。

诀窍,您需要覆盖资产文件夹中SQLiteOpenHelper复制数据库文件的行为,而不是创建您的数据库。

在这篇blog帖子中,我详细解释了覆盖此行为的过程。但这是最终的代码。

使用Repository类,正常情况下使用SQLiteOpenHelper

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();
        }
    }
}
}

答案 1 :(得分:1)

您需要做的就是将sqlite数据库放在您的assets文件夹中,然后当您的应用程序第一次启动时,将数据库复制到SDCard。

Here is a great description如何做到这一点。

答案 2 :(得分:0)

Android使用SQLite的内部数据库。如果您想使用外部SQLite数据库(或任何其他外部数据库),您将需要使用类似HHTP代理的东西。这是一个提供更多信息的链接:https://stackoverflow.com/a/4124829/1852466