• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help assets folder not acessing database file on real device

Here is my script
For some reason it works on the emulator but when i run it on the phone it does not access the database file. This an example based on the book beginning android development.

method tried to fix problem

1. I added the new table called android_meta
2. I tried altering the database and trying outStream.flush
3. I think from reading the log cat that there could be a problem with my version of sq lite. Should i upgrade my phone.

Could some please show me how i can fix my database I would be very appreciative.

Here is the logcat

06-19 17:55:33.124: E/SqliteDatabaseCpp(547): sqlite3_open_v2("nullMyDB", &handle, 1, NULL) failed
06-19 17:55:33.144: E/SQLiteDatabase(547): Failed to open the database. closing it.
06-19 17:55:33.144: E/SQLiteDatabase(547): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
06-19 17:55:33.144: E/SQLiteDatabase(547): at alex.android.test.db.DBAdapter$DatabaseHelper.checkDataBase(DBAdapter.java:107)
06-19 17:55:33.144: E/SQLiteDatabase(547): at alex.android.test.db.DBAdapter$DatabaseHelper.<init>(DBAdapter.java:49)
06-19 17:55:33.144: E/SQLiteDatabase(547): at alex.android.test.db.DBAdapter.<init>(DBAdapter.java:40)
06-19 17:55:33.144: E/SQLiteDatabase(547): at alex.android.test.db.AndroidtestdbActivity.onCreate(AndroidtestdbActivity.java:26)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.app.Activity.performCreate(Activity.java:4465)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.os.Looper.loop(Looper.java:137)
06-19 17:55:33.144: E/SQLiteDatabase(547): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 17:55:33.144: E/SQLiteDatabase(547): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:55:33.144: E/SQLiteDatabase(547): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:55:33.144: E/SQLiteDatabase(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 17:55:33.144: E/SQLiteDatabase(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 17:55:33.144: E/SQLiteDatabase(547): at dalvik.system.NativeStart.main(Native Method)
06-19 17:55:33.144: V/db log(547): database does't exist
06-19 17:55:33.204: D/AndroidRuntime(547): Shutting down VM
06-19 17:55:33.204: W/dalvikvm(547): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-19 17:55:33.244: E/AndroidRuntime(547): FATAL EXCEPTION: main
06-19 17:55:33.244: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{alex.android.test.db/alex.android.test.db.AndroidtestdbActivity}: java.lang.NullPointerException
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:137)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 17:55:33.244: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:55:33.244: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:55:33.244: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 17:55:33.244: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 17:55:33.244: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
06-19 17:55:33.244: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
06-19 17:55:33.244: E/AndroidRuntime(547): at alex.android.test.db.DBAdapter$DatabaseHelper.copyDataBase(DBAdapter.java:88)
06-19 17:55:33.244: E/AndroidRuntime(547): at alex.android.test.db.DBAdapter$DatabaseHelper.<init>(DBAdapter.java:54)
06-19 17:55:33.244: E/AndroidRuntime(547): at alex.android.test.db.DBAdapter.<init>(DBAdapter.java:40)
06-19 17:55:33.244: E/AndroidRuntime(547): at alex.android.test.db.AndroidtestdbActivity.onCreate(AndroidtestdbActivity.java:26)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.Activity.performCreate(Activity.java:4465)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-19 17:55:33.244: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-19 17:55:33.244: E/AndroidRuntime(547): ... 11 more




Here is the script that is meant to upload the databae files data

try {
String destPath = "/data/data/" + getPackageName() + "/databases/MyDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}



public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}



public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "MyDB";
private static final String DATABASE_TABLE = "contacts";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE =
"create table contacts (_id integer primary key autoincrement, "
+ "name text not null, email text not null);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}

//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
DBHelper.close();
}

//---insert a contact into the database---
public long insertContact(String name, String email)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_EMAIL, email);
return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular contact---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

//---retrieves all the contacts---
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_EMAIL}, null, null, null, null, null);
}

//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_EMAIL}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

//---updates a contact---
public boolean updateContact(long rowId, String name, String email)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_EMAIL, email);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
 
Back
Top Bottom