Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Importation base SQLite

Featured Replies

Posté(e)

Bonjour à tous,

J'ai une base de données SQLite que je voudrais dans deux applis (une iOS et une Android)

Coté iOS tout se passe très bien, par contre côté Android je n'arrive même pas à exécuter la moindre requête.

J'ai cru comprendre que même en mettant le fichier .db dans le dossier assets, ça ne suffisait pas à inserer le fichier dans le package, du coup j'ai fait un fichier DataBaseHelper.java qui est censé s'en charger

Voici mon code :

DataBaseHelper bdd = new DataBaseHelper(this);
bdd = new DataBaseHelper(this);

try {bdd.createDataBase();} catch (IOException ioe) {throw new Error("Unable to create database");}
try {bdd.openDataBase();}catch(SQLException sqle){throw sqle;}

Cursor result = bdd.getReadableDatabase().query(false, "sujet", new String[] {"lieu"}, null, null, null, null, null, null);
String a = result.getString(0);
textView1.setText("Visite du musée"+a);

sachant que le bug survient à la ligne

Cursor result = bdd.getReadableDatabase().query(false, "sujet", new String[] {"lieu"}, null, null, null, null, null, null);

qui donne un sympathique Force close

en gros à la base je voulais compter le nombre de lieux (colonne lieu, de type CHAR(1), représenté par une lettre) dans la table "sujet", ma requête serait donc :

SELECT COUNT(*) FROM (SELECT DISTINCT lieu FROM sujet);

Comment faire pour extraire un entier proprement en java avec cette requete ?

Dans l'attente de vos réponses et commentaires, merci

finalement j'ai trouvé

si ça peut en aider d'autres, voici mon code :

DataBaseHelper bdd = new DataBaseHelper(this);
       bdd = new DataBaseHelper(this);

       try {bdd.createDataBase();} catch (IOException ioe) {throw new Error("Unable to create database");}
       try {bdd.openDataBase();}catch(SQLException sqle){throw sqle;}

       Cursor mCount= bdd.getReadableDatabase().rawQuery("select count(*) from (select distinct lieu from sujet)", null);
       mCount.moveToFirst();
       int a= mCount.getInt(0);
       mCount.close();

       textView1.setText("Visite du musée"+a);

Modifié par Zenos77

  • 1 month later...
Posté(e)

Il faut recopier physiquement la base de asset vers un repertoire de l'appli. par exemple en utilisant des trucs comme ça :

private static final String DB_PATH = "/data/data/com.ton.appli/databases/";
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_PATH+DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e) {
Log.d("tonApp", "database doesn't exist yet");
}
if (checkDB != null){
checkDB.close();
}
return checkDB != null ? true:false;
}

public void openDatabase() throws SQLException {
myDataBase = SQLiteDatabase.openDatabase(DB_PATH+DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void createDatabase() throws IOException {

boolean dbExist = checkDatabase();
if (!dbExist) {
if (!lock) {
lock = true;

this.getReadableDatabase();
try {
 copyDatabase();
 this.close();
}
catch (IOException e) {
 e.printStackTrace();
 throw new Error ("Error copying database");
}
}
}
}
private void copyDatabase() throws IOException {

String	 outFileName = DB_PATH+DB_NAME;
InputStream xaa = myContext.getAssets().open(DB_NAME);


FileOutputStream myOutput = new FileOutputStream(outFileName);
byte [] buffer = new byte [1024];
int	 length;
while ((length = xaa.read(buffer)) >0) {
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
xaa.close();

lock = false;
}

Modifié par Mitsuaki

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.