IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Erreur "could not open database"


Sujet :

Android

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2015
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2015
    Messages : 9
    Points : 8
    Points
    8
    Par défaut Erreur "could not open database"
    Bonjour,

    Une question qui va peut-être paraître simple pour certains
    mais j'ai pas pu me connecter à la base de données et une erreur se déclenche à chaque fois et la voila:
    "android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database"

    voici le code:

    Classe : DataBaseHelper.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
     
    package com.example.hanoun.myapplication;
     
     
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.database.sqlite.SQLiteOpenHelper ;
     
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.sql.SQLException;
     
    /**
     * Created by FILALI on 15/07/2015.
     */
     
    public class DataBaseHelper extends SQLiteOpenHelper {
     
        private static String DB_PATH = "//data/data/com.example.hanoun.myapplication/databases/";
        private static String DB_NAME = "clerk_project.sqlite";
        private SQLiteDatabase myDataBase;
        private final Context myContext;
     
        public DataBaseHelper(Context context ) {
     
            super(context,DB_NAME,null,1);
            this.myContext=context;
        }
     
        public void createDataBase() throws IOException {
     
            boolean dbExist = checkDataBase();
     
            if(dbExist){
                //do nothing - database already exist
            }else{
     
                //By calling this method and empty database will be created into the default system path
                //of your application so we are gonna be able to overwrite that database with our database.
                this.getReadableDatabase();
     
                try {
     
                    copyDataBase();
     
                } catch (IOException e) {
     
                    throw new Error("Error copying database");
     
                }
            }
     
        }
     
     
     
        private boolean checkDataBase() {
            SQLiteDatabase checkDB = null;
     
            try{
                String myPath = DB_PATH + DB_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
     
            }catch(SQLiteException e){
     
                //database does't exist yet.
     
            }
     
            if(checkDB != null){
     
                checkDB.close();
     
            }
     
            return checkDB != null ? true : false;    }
     
        private void copyDataBase() throws IOException{
     
            //Open your local db as the input stream
            InputStream myInput = myContext.getAssets().open(DB_NAME);
     
            // Path to the just created empty db
            String outFileName = DB_PATH + DB_NAME;
     
            //Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);
     
            //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }
     
            //Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
     
        public void openDataBase() throws SQLException {
     
            //Open the database
            String myPath = DB_PATH + DB_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
     
        }
     
        @Override
        public synchronized void close() {
     
            if(myDataBase != null)
                myDataBase.close();
     
            super.close();
     
        }
     
     
        @Override
        public void onCreate(SQLiteDatabase db) {
     
        }
     
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     
        }
    }


    aidez moi s'il vous plait
    merci d'avance

  2. #2
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    peux-tu nous montrer le code?
    cela peux-etre du a une permission manquante.
    Eric

  3. #3
    Futur Membre du Club
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2015
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2015
    Messages : 9
    Points : 8
    Points
    8
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
     
    package com.example.hanoun.myapplication;
     
     
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.database.sqlite.SQLiteOpenHelper ;
     
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.sql.SQLException;
     
    /**
     * Created by FILALI on 15/07/2015.
     */
     
    public class DataBaseHelper extends SQLiteOpenHelper {
     
        private static String DB_PATH = "//data/data/com.example.hanoun.myapplication/databases/";
        private static String DB_NAME = "clerk_project.sqlite";
        private SQLiteDatabase myDataBase;
        private final Context myContext;
     
        public DataBaseHelper(Context context ) {
     
            super(context,DB_NAME,null,1);
            this.myContext=context;
        }
     
        public void createDataBase() throws IOException {
     
            boolean dbExist = checkDataBase();
     
            if(dbExist){
                //do nothing - database already exist
            }else{
     
                //By calling this method and empty database will be created into the default system path
                //of your application so we are gonna be able to overwrite that database with our database.
                this.getReadableDatabase();
     
                try {
     
                    copyDataBase();
     
                } catch (IOException e) {
     
                    throw new Error("Error copying database");
     
                }
            }
     
        }
     
     
     
        private boolean checkDataBase() {
            SQLiteDatabase checkDB = null;
     
            try{
                String myPath = DB_PATH + DB_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
     
            }catch(SQLiteException e){
     
                //database does't exist yet.
     
            }
     
            if(checkDB != null){
     
                checkDB.close();
     
            }
     
            return checkDB != null ? true : false;    }
     
        private void copyDataBase() throws IOException{
     
            //Open your local db as the input stream
            InputStream myInput = myContext.getAssets().open(DB_NAME);
     
            // Path to the just created empty db
            String outFileName = DB_PATH + DB_NAME;
     
            //Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);
     
            //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }
     
            //Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
     
        public void openDataBase() throws SQLException {
     
            //Open the database
            String myPath = DB_PATH + DB_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
     
        }
     
        @Override
        public synchronized void close() {
     
            if(myDataBase != null)
                myDataBase.close();
     
            super.close();
     
        }
     
     
        @Override
        public void onCreate(SQLiteDatabase db) {
     
        }
     
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
     
     
    package com.example.hanoun.myapplication;
     
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
     
    import java.sql.SQLException;
     
     
    /**
     * Created by Hanoun on 06/07/2015.
     */
    public class client extends Fragment  {
     
        private Context context ;
        private EditText tf1,tf2,tf3,tf4,tf5,tf6;
        private String str1,str2,str3,str4,str5,str6;
        private View v ;
        private Button button;
     
        private SQLiteDatabase mDataBase;
        private DataBaseHelper MyDB ;
     
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
             v= inflater.inflate(R.layout.client_layout,container,false);
     
            tf1 = (EditText) v.findViewById(R.id.tfName);
            tf2 = (EditText) v.findViewById(R.id.tfAdr);
            tf3 = (EditText) v.findViewById(R.id.tfCAP);
            tf4 = (EditText) v.findViewById(R.id.tfVille);
            tf5 = (EditText) v.findViewById(R.id.tfProv);
            tf6 = (EditText) v.findViewById(R.id.tfCountry);
     
            str1= tf1.getText().toString() ;
            str2=tf2.getText().toString() ;
            str3= tf3.getText().toString() ;
            str4=tf4.getText().toString() ;
            str5= tf5.getText().toString() ;
            str6=tf6.getText().toString();
     
            button= (Button) v.findViewById(R.id.addBtn);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    try {
                        AddData();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            });
     
            return v ;
        }
     
        public String getStr1() {
            return str1;
        }
        public String getStr2() {
            return str2;
        }
        public String getStr3() {
            return str3;
        }
        public String getStr4() {
            return str4;
        }
        public String getStr5() {
            return str5;
        }
        public String getStr6() {
            return str6;
        }
     
            public  void AddData () throws SQLException
        {
            MyDB = new DataBaseHelper(v.getContext());
            try {
                MyDB.createDataBase();
            }
            catch (Exception  e){ System.out.println(e);}
     
            try {
                MyDB.openDataBase();
            }
            catch(SQLException sqle){
     
                throw sqle;
            }
     
            SQLiteDatabase db = MyDB.getWritableDatabase();
     
            ContentValues values = new ContentValues();
            values.put("Name",    str1  );
            values.put("Address", str2  );
            values.put("CAP",     str3  );
            values.put("City",    str4  );
            values.put("Province",str5  );
            values.put("Country", str6  );
     
            // Inserting Row
            db.insert("Clients", null, values);
            db.close(); // Closing database connection
        }
     
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     
               }
     
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
     
                }
        @Override
        public void onStart() {
            super.onStart();
     
        }
     
        @Override
        public void onPause() {
            super.onPause();
        }
     
        @Override
        public void onResume() {
            super.onResume();
        }
     
    }

  4. #4
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,

    peux-tu faire ce test:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    String myPath = DB_PATH + DB_NAME;
     
    File dbFile = myContext.getDatabasePath(myPath);
    if(dbFile.exists())
     checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    else
      System.out.println("Fichier introuvable");
    Eric

  5. #5
    Futur Membre du Club
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2015
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2015
    Messages : 9
    Points : 8
    Points
    8
    Par défaut
    merci en fait je peux me connecter maintenant à la base mais l'insertion ne se produit pas
    et m'affiche dans l'erreur que la colonne n'existe pas!!!
    Que dois_je faire?
    merci

  6. #6
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    cela veut dire que une ou plusieurs colonnes de ta requete n´existe(nt) pas dans la base de données.

    essaies ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
      select Name,Address,CAP, City,Province, Country
      from clients;
    et donnes nous le resultat.

    Eric

Discussions similaires

  1. Réponses: 1
    Dernier message: 08/02/2013, 08h50
  2. Réponses: 3
    Dernier message: 30/03/2005, 23h15
  3. Erreur : "could not create process"
    Par spéculteur dans le forum C++
    Réponses: 3
    Dernier message: 29/03/2005, 11h31
  4. [CVS] Problème de commit (Could not open lock file)
    Par 1tox dans le forum Eclipse Java
    Réponses: 4
    Dernier message: 19/10/2004, 11h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo