Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite database corrupted when pulling with ADB

I have a big problem with the SQLite database on Android. In a file DatabaseContra.java I have

public static class HeartRateEntry implements BaseColumns {
    public static final String TABLE_NAME = "heart_rate_readings";
    public static final String COLUMN_NAME_SESSION_ID = "session_id";
    public static final String COLUMN_NAME_SENSOR_ID= "sensor_id";
    public static final String COLUMN_NAME_TIMESTAMP_NANO = "timestamp_nano";
    public static final String COLUMN_NAME_TIMESTAMP_MILLI = "timestamp_milli";
    public static final String COLUMN_NAME_HR = "heart_rate";
    public static final String COLUMN_NAME_RR = "rr_interval";
}

In a DatabaseHelper class inheriting from SQLiteOpenHelper I have the create statement for the table:

private static final String CREATE_TABLE_HEART_RATE_READINGS =
        "CREATE TABLE " + DatabaseContract.HeartRateEntry.TABLE_NAME + " (" +
                DatabaseContract.HeartRateEntry._ID + " INTEGER PRIMARY KEY," +
                DatabaseContract.HeartRateEntry.COLUMN_NAME_SESSION_ID + " INTEGER NOT NULL," +
                DatabaseContract.HeartRateEntry.COLUMN_NAME_SENSOR_ID + " INTEGER NOT NULL," +
                DatabaseContract.HeartRateEntry.COLUMN_NAME_TIMESTAMP_NANO + " INTEGER NOT NULL," +
                DatabaseContract.HeartRateEntry.COLUMN_NAME_TIMESTAMP_MILLI + " INTEGER NOT NULL," +
                DatabaseContract.HeartRateEntry.COLUMN_NAME_HR + " INTEGER," +
                DatabaseContract.HeartRateEntry.COLUMN_NAME_RR + " INTEGER);";

In the public void onCreate(SQLiteDatabase db) method I'm creating the table db.execSQL(CREATE_TABLE_HEART_RATE_READINGS);

No I try to insert every second one row through

SQLiteOpenHelper mDatabaseHelper = new DatabaseHelper(MainActivity.context);
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseContract.HeartRateEntry.COLUMN_NAME_SESSION_ID, MainActivity.sessionDbID);
contentValues.put(DatabaseContract.HeartRateEntry.COLUMN_NAME_SENSOR_ID, MainActivity.polarDbID);
contentValues.put(DatabaseContract.HeartRateEntry.COLUMN_NAME_TIMESTAMP_NANO, timeNano);
contentValues.put(DatabaseContract.HeartRateEntry.COLUMN_NAME_TIMESTAMP_MILLI, timeMilli);
contentValues.put(DatabaseContract.HeartRateEntry.COLUMN_NAME_HR, hr);
contentValues.put(DatabaseContract.HeartRateEntry.COLUMN_NAME_RR, rr[i]);
db.insert(DatabaseContract.HeartRateEntry.TABLE_NAME, null, contentValues);

The database is used in a service but I'm using the context of the MainActivity (which I have stored in a static variable there). I'm using it in a service but I just use the context of the MainActivity. I'm leaving the connection open to the database and only close it at the very end. I also only use one connection to the database.

I'm not getting any error message while execution. But then I'm pulling the database from my PC with

adb -d shell "run-as com.example cat /data/data/com.example/databases/sensors.db" > C:\Users\myName\Desktop\sensors.db

When I now open the database with SQLiteStudio I'm getting the following error message when I want to access the table:

Could not load data for table heart_rate_readings. Error details: Error while executing SQL query on database 'sensors': database disk image is malformed

What could the error be? I'm completely stuck and have no clue what it could be. I'm using Android 5.1.

I have also defined other tables (user information etc.). These table work (but there I only enter records once and not every second).

like image 906
machinery Avatar asked Feb 05 '26 03:02

machinery


1 Answers

The command for pulling the database from the device:

adb -d pull /data/data/com.example/databases/sensors.db

If you can't access the database like this, but run-as works for you, then copy the database to publicly accessible location (e.g. /) and then use pull.

like image 56
Vasiliy Avatar answered Feb 07 '26 23:02

Vasiliy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!