在android Activity(Android SQLite数据库)中导入.SQLite文件

Zee*_*hry 0 database sqlite android

我已经成功创建了一个数据库程序.基本上我正在处理一个小复杂的数据库,所以需要在SQLITE MANAGER中创建数据库并使用我的android代码导入/链接该数据库.请建议我做这个教程.或任何其他帮助我的代码.谢谢

这是我的数据库代码现在我要导入mYdataBase.sqlite文件.

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contactsManager";
    private static final String TABLE_CONTACTS = "contacts";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }
Run Code Online (Sandbox Code Playgroud)

Har*_*ara 5

如果你想从资产文件夹导入你的数据库文件比下面的代码可以帮助你

void checkDB() throws Exception {
        try {
            SQLiteDatabase dbe = SQLiteDatabase
                    .openDatabase(
                            "/data/data/com.yourpackagename/databases/yourfile.sqlite",
                            null, 0);
            Log.d("opendb", "EXIST");
            dbe.close();
        } catch (Exception e) {

            AssetManager am = getApplicationContext().getAssets();
            OutputStream os = new FileOutputStream(
                    "/data/data/com.yourpackagename/databases/yourfile.sqlite");
            byte[] b = new byte[100];

            int r;
            InputStream is = am.open("yourfile.sqlite");
            while ((r = is.read(b)) != -1) {
                os.write(b, 0, r);
            }
            Log.i("DATABASE_HELPER", "Copying the database ");
            is.close();
            os.close();
        }
Run Code Online (Sandbox Code Playgroud)