1

なぜ私のデータベースがエラーを出すのか.私を助けてください: - 私はでデータベースを作成していますsdcard.

私が間違いをしている場所。

public class SDcardDatabase extends SQLiteOpenHelper
{

    public static final String  DATABASE_FILE_PATH = "/sdcard";
    public static final String  DATABASE_NAME = "testDatabase";
    public final static String NAME ="name";
    public final static String ADDRESS ="address";
    public final static String CITY ="city";

    public SDcardDatabase(final Context context) {
        super(context,Environment.getExternalStorageDirectory()+File.separator+DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        try
        {  
            db.execSQL( "CREATE TABLE mylistdata(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT,city TEXT);");

        }

        catch (SQLiteException ex)
        {
            Log.e("Hello", "error -- " + ex.getMessage(), ex);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP Table mylistdata");
        onCreate(db);
    }
}

これをアクティビティで呼び出します。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mydb = new SDcardDatabase(this);
        sqldb = mydb.getWritableDatabase();
        sqldb.close();
    }

エラーログ:-

01-29 10:03:46.534: W/dalvikvm(8185): threadid=1: thread exiting with uncaught exception (group=0x40014760)
01-29 10:03:46.548: E/AndroidRuntime(8185): FATAL EXCEPTION: main
01-29 10:03:46.548: E/AndroidRuntime(8185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sdcard/com.example.sdcard.SDcardDBActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.os.Looper.loop(Looper.java:126)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.ActivityThread.main(ActivityThread.java:3997)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at java.lang.reflect.Method.invoke(Method.java:491)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at dalvik.system.NativeStart.main(Native Method)
01-29 10:03:46.548: E/AndroidRuntime(8185): Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:978)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:951)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1016)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:726)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at com.example.sdcard.SDcardDBActivity.onCreate(SDcardDBActivity.java:20)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
01-29 10:03:46.548: E/AndroidRuntime(8185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
4

1 に答える 1

3

追加してみてください:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-01-29T04:37:58.040 に答える