1

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

4

3 回答 3

2

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

所以诀窍是,您需要重写从 assets 文件夹SQLiteOpenHelper复制数据库文件的行为,而不是创建数据库。

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

像平常一样使用这个RepositorySQLiteOpenHelper

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();
        }
    }
}
}
于 2013-05-15T11:33:12.853 回答
1

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

这是如何做到这一点的一个很好的描述。

于 2013-05-15T01:36:44.780 回答
0

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

于 2013-05-15T01:30:20.580 回答