Jump to content

Archived

This topic is now archived and is closed to further replies.

TsCyrille

[Résolu] Creer une base de données sur la SD Card

Recommended Posts

Bonjour

J'essaye de créer une base de données sur la SD Card mais je n'y arrive pas. Voilà mon code :

    public class DatabaseHelper extends SQLiteOpenHelper {

       Context    context;

       public DatabaseHelper(Context context) {
           super(context, "/sdcard/my_folder/my_base", null, 1);
           this.context = context;
       }

       @Override
       public void onCreate(SQLiteDatabase db) {
       }

       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       }
   }

Le LocCat me retourne cela :

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): Uncaught handler: thread main exiting due to uncaught exception

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): java.lang.RuntimeException: Unable to start activity ComponentInfo{ts.S9000.planning_exterieur/ts.S9000.planning_exterieur.Libelles}: java.lang.IllegalArgumentException: File /sdcard/technic-soft/planning_exterieur/libelles contains a path separator

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ActivityThread.access$2200(ActivityThread.java:119)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.os.Handler.dispatchMessage(Handler.java:99)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.os.Looper.loop(Looper.java:123)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ActivityThread.main(ActivityThread.java:4363)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at java.lang.reflect.Method.invokeNative(Native Method)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at java.lang.reflect.Method.invoke(Method.java:521)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at dalvik.system.NativeStart.main(Native Method)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): Caused by: java.lang.IllegalArgumentException: File /sdcard/technic-soft/planning_exterieur/libelles contains a path separator

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ApplicationContext.makeFilename(ApplicationContext.java:1444)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:472)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at ts.S9000.DB.DBAdapter.open(DBAdapter.java:39)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at ts.S9000.planning_exterieur.Libelles.onCreate(Libelles.java:52)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

08-26 16:44:37.477: ERROR/AndroidRuntime(23576): ... 11 more

A priori il ne faut pas de "/" dans le nom de la base... Comment faire alors pour créer cette base sur la SD Card ?

Merci d'avance

Share this post


Link to post
Share on other sites

J'ai réussi à trouver.... Voici comment j'ai défini ma classe DatabaseHelper :

    public class DatabaseHelper extends SQLiteOpenHelper {

       String NAME_DB;
       String PATH_DB;
       Context    context;

       public DatabaseHelper(Context context, String _pathDB, String _nameDB) {
           super(context, _nameDB, null, 1);

           this.context = context;
           NAME_DB = _nameDB;
           PATH_DB = _pathDB;
       }

       @Override
       public void onCreate(SQLiteDatabase db) {
       }

       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       }

       public SQLiteDatabase openDataBase() throws SQLException{
           boolean continu = true;
           SQLiteDatabase dbRetour = null;

           try {
               if (!checkDataBase()) {
                   SQLiteDatabase dbCreate = SQLiteDatabase.openOrCreateDatabase(PATH_DB + NAME_DB, null);

                   if (dbCreate != null) {
                       dbCreate.execSQL("create table my_table (_id integer primary key autoincrement, col1 text not null);");            
                       dbCreate.close();
                   } else
                       continu = false;
               }

               if (continu)
                   dbRetour = SQLiteDatabase.openDatabase(PATH_DB + NAME_DB, null, SQLiteDatabase.OPEN_READWRITE);
           } catch (Exception e) {

           }

           return dbRetour;
       }

       private boolean checkDataBase() {
           SQLiteDatabase checkDB = null;

           try{
               checkDB = SQLiteDatabase.openDatabase(PATH_DB + NAME_DB, null, SQLiteDatabase.OPEN_READONLY);
           }catch (SQLiteException e) {
               //database does't exist yet.
           }

           if (checkDB != null)
               checkDB.close();

           return checkDB != null ? true : false;
       }

   }

Share this post


Link to post
Share on other sites





×
×
  • Create New...