不是错误(代码0):无法以读/写模式打开数据库

时间:2015-04-26 12:33:16

标签: java android sql sqlite

我使用Android应用程序创建了一个sqlite数据库,并将其存储在我的外部磁盘中。数据库名称为sensor.sqlite,文件路径为

Environment.getExternalStorageDirectory()+ File.separator + "data" + File.separator +"sensor.sqlite"

然而在这行代码中

 this.getWritableDatabase();

例外 - >不是错误(代码0):无法以读/写模式打开数据库。

我已阅读相关帖子

getWritableDatabase() VS getReadableDatabase()

Open Database for ReadWirte not possible on specific device

但我还没有找到答案。

你知道吗?谢谢。

此致 麦

这是我的Android清单文件。

<?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>

这是我的代码。

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();
}

1 个答案:

答案 0 :(得分:1)

您是否添加了读/写File Permission

AndroidManifest中添加权限:

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