在Android上的SDCard上打开外部创建的SQLite数据库

时间:2011-12-05 09:05:30

标签: android database sqlite

我正在编写一个从相机解析SD卡上的数据库的应用程序,我无法弄清楚如何在Android上运行它,我不断“尝试编写只读数据库”异常。数据库不是只读的,我的清单中设置了WRITE_EXTERNAL_STORAGE权限。

我的代码段:

public class DatabaseHelper 
{ 

public static final String  DATABASE_NAME      = "MISC/EREVIEW.DB"; 
 public static final String  SHARE_TABLE         = "Share"; 
 public static final String  PICTURE_TABLE   = "Picture"; 

private SQLiteDatabase      database; 
 private File sdDir = Environment.getExternalStorageDirectory(); 

public DatabaseHelper() 
 { 
     try 
     { 
         database = SQLiteDatabase.openDatabase(sdDir + 
File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE); 
     } 
     catch (SQLiteException ex) 
     { 
         Log.e(TAG, "error -- " + ex.getMessage(), ex); 
     } 
     finally 
     { 
         DBUtil.safeCloseDataBase(database); 
     } 
 }

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mg.keas"
    android:versionCode="1"
    android:versionName="1.0" >

     <uses-sdk android:minSdkVersion="9" />
     <uses-permission android:name="WRITE_EXTERNAL_STORAGE"/>

     <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
         <activity
            android:label="@string/app_name"
            android:name=".KDEASButtonActivity" >
             <intent-filter >
                 <action android:name="android.intent.action.MAIN" />

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>

</manifest>

callstack表示在添加Android元数据表时遇到问题。

    12-04 19:51:12.665: I/Database(842): sqlite returned: error code = 8, 
        msg = prepared statement aborts at 37: [CREATE TABLE IF NOT EXISTS 
        android_metadata (locale TEXT)] 
        12-04 19:51:12.665: E/Database(842): CREATE TABLE android_metadata 
        failed 
        12-04 19:51:12.795: E/Database(842): Failed to setLocale() when 
        constructing, closing the database 
        12-04 19:51:12.795: E/Database(842): 
        android.database.sqlite.SQLiteException: attempt to write a readonly 
        database 
        12-04 19:51:12.795: E/Database(842):    at 
        android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java: 
        1967) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java: 
        1835) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java: 
        820) 
        12-04 19:51:12.795: E/Database(842):    at 
        com.mg.keas.DatabaseHelper.<init>(DatabaseHelper.java:29) 
        12-04 19:51:12.795: E/Database(842):    at 
        com.mg.keas.KDEASButtonActivity.onCreate(KDEASButtonActivity.java:28) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 
        1047) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 
        1586) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 
        1638) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.app.ActivityThread.access$1500(ActivityThread.java:117) 
        12-04 19:51:12.795: E/Database(842):    at android.app.ActivityThread 
        $H.handleMessage(ActivityThread.java:928) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.os.Handler.dispatchMessage(Handler.java:99) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.os.Looper.loop(Looper.java:123) 
        12-04 19:51:12.795: E/Database(842):    at 
        android.app.ActivityThread.main(ActivityThread.java:3647) 
        12-04 19:51:12.795: E/Database(842):    at 
        java.lang.reflect.Method.invokeNative(Native Method) 
        12-04 19:51:12.795: E/Database(842):    at 
        java.lang.reflect.Method.invoke(Method.java:507) 
        12-04 19:51:12.795: E/Database(842):    at 
        com.android.internal.os.ZygoteInit 
        $MethodAndArgsCaller.run(ZygoteInit.java:839) 
        12-04 19:51:12.795: E/Database(842):    at 
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
        12-04 19:51:12.795: E/Database(842):    at 
        dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

这是一个非常古老的帖子,但没有答案,所以试着替换

database = SQLiteDatabase.openDatabase(sdDir + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE); 

database = SQLiteDatabase.openDatabase(sdDir + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS);

设置标志 SQLiteDatabase.NO_LOCALIZED_COLLATORS 后,您将忽略在数据库中创建android_metadata表。 这就是我设法从SD CARD打开我的数据库的方式。

答案 1 :(得分:0)

如果您的数据库文件直接在SD卡中,则替换

public static final String DATABASE_NAME =“MISC / EREVIEW.DB”;

用这个

public static final String DATABASE_NAME =“sdcard / EREVIEW.DB”;

让我知道这是否有效。

答案 2 :(得分:0)

尝试将此添加到清单

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