Reputation: 927
I created a sqlite database using an android app and I stored it in my external disk. The database name is sensor.sqlite and the file path is
Environment.getExternalStorageDirectory()+ File.separator + "data" + File.separator +"sensor.sqlite"
However in this line of code
this.getWritableDatabase();
An exception -> not an error (code 0): Could not open the database in read/write mode was thrown.
I have read related posts
getWritableDatabase() VS getReadableDatabase()
Open Database for ReadWirte not possible on specific device
But I haven't found an answer.
Do you have any idea? Thank you.
Regards, Jimmy
This is my android manifest file.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<activity
android:name="example.com.sensor.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="example.com.sensor.SensorActivity"
android:screenOrientation="portrait">
</activity>
</application>
This is my code.
public SqlController(Context context) {
super(context,
Environment.getExternalStorageDirectory() + File.separator + "data" + File.separator +
"sensor.sqlite"
, null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
String x = "CREATE TABLE [sport] ([id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,";
x += " [accx] REAL,";
x += " [accy] REAL,";
x += " [accz] REAL,";
x += " [geox] REAL,";
x += " [geoy] REAL,";
x += " [geoz] REAL,";
x += " [activity] TEXT,";
x += " [datetime] TIMESTAMP)";
try {
database.execSQL(x);
} catch (Exception ex) {
System.err.println(ex);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
database.execSQL("drop table if exists sport");
onCreate(database);
}
public void insert(HashMap<String, String> queryValues) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("accx", queryValues.get("accx"));
values.put("accy", queryValues.get("accy"));
values.put("accz", queryValues.get("accz"));
values.put("geox", queryValues.get("geox"));
values.put("geoy", queryValues.get("geoy"));
values.put("geoz", queryValues.get("geoz"));
values.put("activity", queryValues.get("activity"));
values.put("datetime", new Date().toString());
database.insert("sport", null, values);
database.close();
}
Upvotes: 0
Views: 6467
Reputation: 983
Do you add Read/Write File Permission
?
Add Permission in AndroidManifest
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 2