sql >> Database teknologi >  >> RDS >> SQLite

kopiering af databasefil fra /aktiver til /data/datamappe i filstifinder - Android

Prøv at bruge nedenstående kode til at kopiere database fra aktiver til data/data/pakkemappe

pakke com.example.myapp;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import android.content.Context;import android .database.Cursor;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log; public class DataBaseHelper1 udvider SQLiteOpenHelper{ private static String DB_PATH ="/data/data/com.example.myapp/databases/"; privat statisk streng DB_NAME ="myDB.sqlite"; privat SQLiteDatabase myDataBase; privat endelig Kontekst myContext; public DataBaseHelper1(Kontekstkontekst) { super(kontekst, DB_NAVN, null, 1); this.myContext =kontekst; } public void createDataBase() kaster IOException{ boolean dbExist =checkDataBase(); if(dbExist) { Log.i("DB...", "database tilgængelig...."); } andet { this.getWritableDatabase(); prøv { copyDataBase(); } catch (IOException e) { throw new Error("Fejl ved kopiering af database"); } Log.i("DB..", "database oprettet..."); } } public boolean checkDataBase(){ SQLiteDatabase checkDB =null; prøv{ String myPath =DB_PATH + DB_NAME; checkDB =SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); }catch(SQLiteException e){ Log.e("CheckDb","DB ikke fundet"); //databasen eksisterer ikke endnu. if(checkDB !=null){ checkDB.close(); } } endelig { if(checkDB !=null){ checkDB.close(); } this.close(); } returner checkDB !=null ? sandt falsk; } private void copyDataBase() kaster IOException{ InputStream myInput =myContext.getAssets().open(DB_NAME); String outFileName =DB_PATH + DB_NAME; OutputStream myOutput =new FileOutputStream(outFileName); byte[] buffer =ny byte[1024]; int længde; while ((length =myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } myOutput.flush(); minOutput.close(); myInput.close(); } public SQLiteDatabase openDataBase() kaster SQLException{ String myPath =DB_PATH + DB_NAME; return myDataBase =SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } @Override public synchronized void close() { if(myDataBase !=null) myDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void getData() { SQLiteDatabase myDB; Cursor cursor; prøv { myDB=this.openDataBase(); cursor=myDB.rawQuery("SELECT * FROM Country_Master",null); if (cursor !=null ) { if (cursor.moveToFirst()) { do {// put your code to get data from cursor }while (cursor.moveToNext()); } } if(cursor !=null) { myDB.close(); cursor.close(); } }catch(SQLException sqle){ throw sqle; } }} 

Indsæt også denne kode under onCreate-metoden i din aktivitet, hvor du vil kopiere databasen..

DataBaseHelper1 myDbHelper =new DataBaseHelper1(MyActivity.this); prøv { myDbHelper.createDataBase(); }catch (IOException e) { // TODO Autogenereret catch block e.printStackTrace(); } endelig { myDbHelper.close(); }


  1. Viser billede fra PostgreSQL-database, bytea

  2. PostgreSQL Meltdown Benchmarks

  3. Oracle 18c Ny funktion:Online ændring af partitionering

  4. Hvordan øger man de maksimale forbindelser i postgres?