How to use the SQLite connection in the Asset folder

How to use the SQLite connection in the Asset folder.
This is called DataBaseHelper. java file

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "TAG";
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
private static String DB_NAME = "ServiceInfo.sqlite";
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 ();
if(!mDataBaseExist)
{
this.getReadableDatabase();
try
{
copyDataBase();
}
catch (IO Exception mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}

private boolean checkDataBase()< br />{
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
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[ 1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);< br /> }
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;< br />}

@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.v(TAG, "UpgradingDatabase, This will drop current database and will recreate it");< br />}
}

Now AdapetrClass.java

public class KamaDBAdapter
{
protected static final String TAG = "TAG";

private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;

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 KamaDBAdapter(Context context)
{
this.mContext = context;
mDbHelper = new DataBaseHelper(mContext);
}

public KamaDBAdapter createDatabase() throws SQLException
{
try
{
mDbHelper .createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException.toString() + "UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;}

public KamaDBAdapter open() throws SQLException
{
try
{
mDbHelper.openDataBase();
mDbHelper. close();
mDb = mDbHelper.getReadableDatabase();
}
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_A DDITIONALDATA, 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;
)< br />
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;
)
< br />public boolean deleteAccount(int mPosition)
{
return mDb.delete(ACCOUNT_TABLE, ACCOUNT_ID + "=" + mPosition, null)> 0;
}
}< /pre>

Now call this class in your main class: something like this:

private static KamaDBAdapter mDbHelper;
mDbHelper = new KamaDBAdapter(Usage .this);
mDbHelper.createDatabase();

Now your database has been copied to the device. You can access the same database you placed in the Asset folder from your local device.

Hope it can help you.

How to use the SQLite connection in the Asset folder.

This is called DataBaseHelper. java file

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "TAG";
private static String DB_PATH = "/data/data/YOUR_PACKAG E/databases/";
private static String DB_NAME = "ServiceInfo.sqlite";
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();
if( !mDataBaseExist)
{
this.getReadableDatabase();
try
{
copyDataBase();
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}

private boolean checkDataBase()
{
SQLiteDatabase mCheckDataBase = null;
try
{
String myPath = DB_PATH + DB_NAME;
mCheckDataBa se = 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
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
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< br />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.v(TAG, "UpgradingDatabase, This will drop current database and will recreate it");
}
}

Now AdapetrClass.java

public class KamaDBAdapter
{
protected static final String TAG = "TAG";

private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;

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 KamaDBAdapter(Context context)
{
this.mContext = context;
mDbHelper = new DataBaseHelper(mContext);
}

public KamaDBAdapter createDatabase() throws SQLException
{< br /> try
{
mDbHelper.createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException. toString() + "UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}

public KamaDBAdapter open () throws SQLException
{
try
{
mDbHelper.openDataBase();
mDbHelper.close();
mDb = mDbHelper.getReadableDatabase() ;
}
catch (SQLException mSQLException)
{
Log.e(TAG, mSQLException.t oString());
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;< br />}

public boolean deleteAccount(int mPosition)
{
return mDb.delete(ACCOUNT_TABLE, ACCOUNT_ID + "=" + mPosition, null)> 0;
}
}

Now call this class in your main class: something like this:

private static KamaDBAdapter mDbHelper;< br />mDbHelper = new KamaDBAdapter(Usage.this);
mDbHelper.createDatabase();

Now your database has been copied to the device. You can access your Asset folder from the local device The same database placed in.

I hope it can help you.

Leave a Comment

Your email address will not be published.