从SD卡导入SQLite数据库到Android - 没有资产文件夹

5
我正在尝试将我的SQLite数据库从SD卡或任何外部位置导入到我的Android应用程序中。我的应用程序需要实现数据库导入,使得数据库模式不会改变,但记录会根据导入的数据库而改变。
例如,在某个时间点,我可能导入DatabaseA,其中包含10条记录,另一个时间点我可能导入具有25条记录的DatabaseA。DatabaseA始终从相同的外部位置导入。
目前我看到的使用assets文件夹的导入方法并没有帮助。我想要导入指向外部位置的数据库。

请查看此链接 - https://dev59.com/-mox5IYBdhLWcg3wl1P4 - Shadab Ansari
因此,这需要将数据库作为应用程序的一部分加载到资产文件夹中。我正在寻找独立于应用程序的解决方案。@ShadabAnsari - Prabha
2个回答

5
我使用以下代码从SD卡导入数据库。
请注意:需要在应用程序内创建一个数据库文件夹才能成功导入数据库。
public void importDB() {

String dir=Environment.getExternalStorageDirectory().getAbsolutePath();
File sd = new File(dir);
File data = Environment.getDataDirectory();
FileChannel source = null;
FileChannel destination = null;
String backupDBPath = "/data/com.example.mine.move/databases/A.db";
String currentDBPath = "A.db";
File currentDB = new File(sd, currentDBPath);
File backupDB = new File(data, backupDBPath);
try {
    source = new FileInputStream(currentDB).getChannel();
    destination = new FileOutputStream(backupDB).getChannel();
    destination.transferFrom(source, 0, source.size());
    source.close();
    destination.close();
    Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    e.printStackTrace();
}

此外,请添加以下权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果您的导入不成功,请降低SDK版本或包含运行时权限。

1

从外部 | 内部目录导入数据库:

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DataBaseName = "dbname";
private static String DB_PATH =  "" ;
SQLiteDatabase database ;
Context  context ;

public DataBaseHelper(Context context) {
    super(context, DataBaseName, null, 1);
    this.context =context ;
    String x = context.getDatabasePath("1").getPath() ;
    x = (String) x.subSequence(0 ,x.length()- 1);
    DB_PATH = x + DataBaseName ;

    if (checkExist()){
        Log.e("DATA_BASE", " Exist");
    }else{
        try {
            createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

boolean checkExist(){
    boolean is = false ;
    try{
        File file = new File(DB_PATH);
        if (file.exists()){
            is= true ;
        }
    }catch (SQLiteException e){
        Log.e("DATABESE_ERR" ,  e.getMessage() ) ;
    }


    return is ;
}

private void createDataBase() throws IOException{
    if (checkExist()){

    }else {
        getReadableDatabase() ;
        try{
            copyDataBase();
        }catch (IOException e){
            Log.e("DATABASE-COPY-ERR", e.getMessage());
        }
    }
}

private void copyDataBase()throws IOException {
    Uri fileUri = "your database file uri" ; 
    File file = new File(fileUri.getPath());
    FileInputStream inputStream = new FileInputStream(file);

    OutputStream outputStream = new FileOutputStream(DB_PATH);

    byte[] buffer = new byte[1024] ;
    int length  =0 ;

    while( (length = inputStream.read(buffer) ) >0 ){
        outputStream.write(buffer ,0 ,length);
    }

    outputStream.flush();
    outputStream.close();
    inputStream.close();

}

public void openDataBase() throws SQLiteException{
    database = SQLiteDatabase.openDatabase(DB_PATH ,null ,SQLiteDatabase.OPEN_READWRITE);
}
public void closeDataBase(){
    if (database!= null){
        database.close();
    }
    try {
        super.clone() ;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
}

}

如何使用这个类:
public class DataBaseInterface {
public DataBaseInterface(Context activity) {
    context = activity;
}

private void openDataBase() {
    try {
        dataBaseHelper = new DataBaseHelper(context);
        dataBaseHelper.openDataBase();
    } catch (Exception e) {
        Log.e("DataBaseError", e.getMessage());
    }
}

private void closeDataBase() {
    dataBaseHelper.close();
}
}

查询数据库的示例方法:
public ArrayList<String> getSomeThing() {
    buffer = new ArrayList<>();
    openDataBase();
    query = "SELECT * FROM table_name";

    cursor = dataBaseHelper.database.rawQuery(query, null);

    for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        buffer.add(i, cursor.getString(0));
    }

    closeDataBase();
    cursor.close();

    return buffer;
}

1
请问您能否提供整个代码,包括调用DataBaseHelper类的部分?@darush dary - Prabha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接