Java 如何将外部大型Sqlite导入我的新android项目

Java 如何将外部大型Sqlite导入我的新android项目,java,android,database,sqlite,Java,Android,Database,Sqlite,我有db xxx.sqlite(8Mo),我想在我的android项目中使用它,所以我使用了这段代码,但它不起作用 Datahelper.java public class Datahelper extends SQLiteOpenHelper { private String dbName; private String db_path; private Context context; public Datahelper(Context context, String dbName)

我有db xxx.sqlite(8Mo),我想在我的android项目中使用它,所以我使用了这段代码,但它不起作用 Datahelper.java

public class Datahelper extends SQLiteOpenHelper {

private String dbName;
private String db_path;
private Context context;


public Datahelper(Context context, String dbName) {
    super(context, dbName, null, 1);
    this.dbName = dbName;
    this.context = context;
    db_path = "/data/data" + context.getPackageName() + "/databases/";

}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 *
 * @return true if it exists, false if it doesn't
 */
public boolean checkExist() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = db_path + dbName;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        e.printStackTrace();
        // database does't exist yet.

    } catch (Exception ep) {
        ep.printStackTrace();
    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public int importIfNotExist() throws IOException {
      int a=0;
    boolean dbExist = checkExist();

    if (dbExist) {
        a=0;
        // do nothing - database already exist
    } else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        this.getReadableDatabase();

        try {

            copyDatabase();
          a=2;
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }
    return a;

}

private void copyDatabase() throws IOException {
    InputStream is = context.getAssets().open(dbName);

    OutputStream os = new FileOutputStream(db_path + dbName);

    byte[] buffer = new byte[4096];
    int length;
    while ((length = is.read(buffer)) > 0) {
        os.write(buffer, 0, length);
    }
    os.flush();
    os.close();
    is.close();
    this.close();
}

@Override
public void onCreate(SQLiteDatabase db) {


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


} }
在我的activity.java中

Datahelper dbHelper = new Datahelper(
            getBaseContext(), "xxx.sqlite");
    try {
        dbHelper.importIfNotExist();
    } catch (IOException e) {
        e.printStackTrace();
    }
我的一些朋友告诉我,我的数据库是非常大的8Mo,我试图把它放在res/raw中,但它不工作,我把它放在assets文件夹中,它不工作。 如果有人能帮忙,请

编辑

检查这个


您可以使用此函数从旧数据库中获取所有数据,将“COLUMN_NAME”替换为您自己的

public boolean consulta(String titulo, String issn){
        String a="",b="",c="";
        boolean respuesta = false;
        cursor = db.rawQuery("select * from "+ TABLA_NAME, null);
        if (cursor.getCount() != 0) {
            if (cursor.moveToFirst()) {
                do {
                    a = cursor.getString(cursor.getColumnIndex("COLUMN_NAME1")); 
                    b = cursor.getString(cursor.getColumnIndex("COLUMN_NAME2"));
                    c = cursor.getString(cursor.getColumnIndex("COLUMN_NAME3"));
                }while (cursor.moveToNext()); 
            }
        }
        respuesta = a+"-"+b+"-"+c;
        cursor.close(); // cierra cursor
        return respuesta;
    }
使用此选项将数据保存在txt中

private void saveData(String data){
        try {
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/dataFile.txt");
            file.createNewFile();
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            myOutWriter.append(data);
            myOutWriter.close();
            fOut.close();
            Toast.makeText(getBaseContext(), "LISTO, se escribio en la tarjeta SD", Toast.LENGTH_SHORT).show();//mensaje de que se escribio
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();// mensaje de error
        }
    }
之后,在新的Android项目中,使用此函数从txt文件获取数据并存储在新数据库中

private void readTxt(File file){
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/dataFile.txt");
        StringBuilder text = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) { 
                text.append(line);
                text.append('\n');
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data = "";
        StringTokenizer st = new StringTokenizer(text,"-");
        while(st.hasMoreTokens()){
            // you can set values to the column as much as you have
            ContentValues contentValues = new ContentValues();
            contentValues.put("COLUMN_NAME1", st.nextToken());
            contentValues.put("COLUMN_NAME2", st.nextToken());
            contentValues.put("COLUMN_NAME3", st.nextToken());
            database.insert(TABLA_PRINCIPAL, null, contentValues); // se inserta en la BD
            Toast.makeText(getBaseContext(), "Datos agregados", Toast.LENGTH_LONG).show(); // muestra mensaje de inserccion satisfactorio
        }
    }

希望这有帮助。

我的android项目中没有它,因为发送给我的一位朋友是用软件“sqlite manger”创建的。好吧,再次检查我的回答。您可能想使用本教程,因为在本例中,他们将db复制到文件夹“资产”中资产不能接受db>1Mo,而我的是8Mofreinds,因为我的sqlite很大,我找到一些建议,告诉我将db溢出并导入资产,但我不知道如何溢出sqlite以及如何导入所有溢出文件。如果使用,您可以将其放入资产文件夹。但是我的db非常大,8moo,资产文件夹不能接受db>1MoI'm不确定Mo是什么类型的单位。你是说MB吗?在我的sqlite“Taille 8Mo”的属性中,我感谢Mo=MB,因为在法语中字节是八位字节