Copy Sqlite database for android application

前端 未结 3 1330
旧时难觅i
旧时难觅i 2021-01-15 23:23

I am trying to use my own created database in my android application using this tutorial http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applicatio

相关标签:
3条回答
  • 2021-01-16 00:10

    The problem with your code is that when you use the getReadableDatabase() function it creates an empty database in the specified location. Take a look at this thread, it provided a solution for me when I had the same issue:

    http://www.anddev.org/networking-database-problems-f29/missing-table-in-sqlite-with-specific-version-of-desire-hd-t50364.html

    0 讨论(0)
  • 2021-01-16 00:13

    Using SQLiteAssetHelper solved the problem.

    0 讨论(0)
  • 2021-01-16 00:20

    I even had the same problem and get really frustrated about this, even I used this tutorial but it didn´t work. I searched many threads about this and find one...and even this doesn´t work...BUT I made a simple change. First, create two classes DatabaseHelper and CopyAdapter:

    DatabaseHelper

        public class DatabaseHelper extends SQLiteOpenHelper{
    private static String TAG = "TAG";
    private static String DB_PATH = "/data/data/PLACE_HERE_YOUR_INTERNAL_PATH/databases/";
    private static String DB_NAME = "PLACE_HERE_YOUR_DATABASE_NAME_WITH_EXTENSION";
    private SQLiteDatabase mDataBase; 
    private final Context mContext;
    
    public DatabaseHelper(Context context) 
    {
        super(context, DB_NAME, null, 1);
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        this.mContext = context;
    }   
    
    public void createDataBase() throws IOException
    {
        boolean mDataBaseExist = checkDataBase();
        Log.d(TAG,"create DB in Helper. Data exists?"+mDataBaseExist);
        if(!mDataBaseExist)
        {
            Log.d(TAG,"get Writable in DatabaseHelper");
            this.getWritableDatabase();
            try 
            {
                Log.d(TAG,"copy Database");
                copyDataBase();
            } 
            catch (IOException mIOException) 
            {Log.d(TAG,"copy not succeed");
                throw new Error("ErrorCopyingDataBase");
    
            }
        }
    }
    
    private boolean checkDataBase()
    {
        SQLiteDatabase mCheckDataBase = null;
        try
        {
            String myPath = DB_PATH + DB_NAME;
            mCheckDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        }
        catch(SQLiteException mSQLiteException)
        {
            Log.e(TAG, "DatabaseNotFound " + mSQLiteException.toString());
        }
    
        if(mCheckDataBase != null)
        {
            mCheckDataBase.close();
        }
        return mCheckDataBase != null;
    }
    
    private void copyDataBase() throws IOException
    {
        Log.d(TAG,"copy");
        InputStream mInput = mContext.getResources().getAssets().open(DB_NAME);
    
        String outFileName = DB_PATH + DB_NAME;
        Log.d(TAG,"Output:"+outFileName);
        File createOutFile = new File(outFileName);
        if(!createOutFile.exists()){
            createOutFile.mkdir();
        }
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer))>0)
        {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }
    
    public boolean openDataBase() throws SQLException
    {
        String mPath = DB_PATH + DB_NAME;
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        return mDataBase != null;
    }
    
    @Override
    public synchronized void close() 
    {
        if(mDataBase != null)
            mDataBase.close();
        super.close();
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) 
    { }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.vTAG, "UpgradingDatabase, This will drop current database and will recreate it");
    }
       }
    

    CopyAdapter

        public class CopyAdapter {
    
    private final Context mContext;
    private SQLiteDatabase mDb;
    private DatabaseHelper mDbHelper;
        private static String TAG = "TAG";
    private static String ACCOUNT_TABLE = "account";
    public static String ACCOUNT_EXTRADATA = "extraData";
    public static String ACCOUNT_ID = "ID";
    public static String ACCOUNT_ADDITIONALDATA = "additionalData";
    public static String ACCOUNT_DATA = "data";
    
    public CopyAdapter(Context context) 
    {
        this.mContext = context;
        mDbHelper = new DatabaseHelper(mContext);
        Log.d(TAG,"done");
    }
    
    public CopyAdapter createDatabase() throws SQLException 
    {
        try 
        {
            Log.d(TAG,"create database");
            mDbHelper.createDataBase();
        } 
        catch (IOException mIOException) 
        {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }
    
    public CopyAdapter open() throws SQLException 
    {
        try 
        {
            Log.d(TAG,"Open");
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getWritableDatabase();
        } 
        catch (SQLException mSQLException) 
        {
            Log.e(TAG, mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }
    
    public void close() 
    {
        mDbHelper.close();
    }
    
    public int countAccountData() 
    {
        Cursor mCoursor = mDb.query(ACCOUNT_TABLE, new String[] {}, null, null, null, null, null);
        int mReturnedCount = mCoursor.getCount();
        mCoursor.close();
        return mReturnedCount;
    }
    
    public long insertData(String mExtra, String mAdditionalData, String mData) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(ACCOUNT_EXTRADATA, mExtra);
        initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData);
        initialValues.put(ACCOUNT_DATA, mData);
        return mDb.insert(ACCOUNT_TABLE, null, initialValues);
    }
    
    public boolean updateData(int mPosition, String mExtra, String mAdditionalData, String mData) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(ACCOUNT_EXTRADATA, mExtra);
        initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData);
        initialValues.put(ACCOUNT_DATA, mData);
        return mDb.update(ACCOUNT_TABLE, initialValues, "ID=" + mPosition, null) > 0;
    }
    
    public String retriveData(int mPosition)
    {
        Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_DATA}, "ID=" + mPosition, null, null, null, null);
        mCursor.moveToFirst();
        String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_DATA));
        mCursor.close();
        return mReturn;
    }
    
    public String retriveAdditionalData(int mPosition)
    {
        Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_ADDITIONALDATA}, "ID=" + mPosition, null, null, null, null);
        mCursor.moveToFirst();
        String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_ADDITIONALDATA));
        mCursor.close();
        return mReturn;
    }
    
    public boolean deleteAccount(int mPosition) 
    {
        return mDb.delete(ACCOUNT_TABLE, ACCOUNT_ID + "=" + mPosition, null) > 0;
    }
       }
    

    And then just call where ever You need it:

       CopyAdapter  mDbHelper = new CopyAdapter(YourActivity.this);
        mDbHelper.createDatabase();
    

    and be sure that You set

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    in your manifest.xml

    I read so many variants for example, put your database file inside another folder in the asset folder, or rename your ending from Your database file to jpg, or put zip your database file before adding it to assets...FORGET ABOUT THAT. The only way I was able to make my app work, was to put the database file without folder or renaming or ziping inside my asset folder. Just only the pure file. Since this point, it worked without problems.

    I don´t know from where I got this examples above, I can´t find the site anymore (but it is here on stackoverflow), but this, with a little workaround and putting the pure database file inside asset folder, was the solution. Hope it helps..

    0 讨论(0)
提交回复
热议问题