Bonjour à tous,
j'ai suivi un tuto très bien fait, j'ai intégré tous cela à mon projet, mais lorsque j'essaie d'ouvrir la BDD, j'ai l'impression qu'il ne la trouve pas !
Mon Schéma Sqlite avec 2 tables :
App.xaml.cs :
Déclaration Table :
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 using System.IO; using Xamarin.Forms; using Xamarin.Essentials; using GenTurfEvo.Views; using GenTurfEvo.Repositories; using System; namespace GenTurfEvo { public partial class App : Application { readonly string dbPath = Path.Combine(FileSystem.AppDataDirectory, "GenTurfEvo.db"); public static PronoRepository PronoRepository { get; set; } public static HippoRepository HippoRepository { get; set; } public App() { InitializeComponent(); PronoRepository = new PronoRepository(dbPath); HippoRepository = new HippoRepository(dbPath); MainPage = new MainPage(); } protected override void OnStart() { } protected override void OnSleep() { } protected override void OnResume() { } } }
dbPath = "/data/user/0/com.companyname.GenTurfEvo/files/GenTurfEvo.db"
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 using SQLite; namespace GenTurfEvo.Models { [Table("Pronostics")] public class Pronostics { [PrimaryKey] public string Id { get; set; } public string DateCourse { get; internal set; } public int NumReunion { get; internal set; } public string Hippodrome { get; internal set; } public int NumCourse { get; internal set; } public int IdCourse { get; internal set; } public int AgeMoyen { get; internal set; } public int Distance { get; internal set; } public int MaBase { get; internal set; } public int Ch1 { get; internal set; } public int Ch2 { get; internal set; } public int Ch3 { get; internal set; } public int Ch4 { get; internal set; } public int Ch5 { get; internal set; } public int PMaBase { get; internal set; } public int PCh1 { get; internal set; } public int PCh2 { get; internal set; } public int PCh3 { get; internal set; } public int PCh4 { get; internal set; } public int PCh5 { get; internal set; } public string FlgFin { get; internal set; } } }
PronoRepository.cs :
Connnexion :
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 using GenTurfEvo.Models; using SQLite; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace GenTurfEvo.Repositories { public class PronoRepository { private readonly SQLiteAsyncConnection connexion; public string StatusMessage { get; set; } public PronoRepository(string dbPath) { connexion = new SQLiteAsyncConnection(dbPath); } public async Task<List<Pronostics>> GetPronosticsAsync() { try { return await connexion.Table<Pronostics>().ToListAsync(); } catch (Exception ex) { StatusMessage = $"Impossible de récupérer les pronostics.\n Erreur : {ex.Message}"; } return new List<Pronostics>(); } public async Task AddPronosticsAsync(string dateCourse, int numReunion, string hippodrome, int numCourse, int idCourse, int ageMoyen, int distance, int maBase, int ch1, int ch2, int ch3, int ch4, int ch5, int pMaBase, int pCh1, int pCh2, int pCh3, int pCh4, int pCh5, string flgFin) { int result = 0; try { result = await connexion.InsertAsync(new Pronostics { DateCourse = dateCourse, NumReunion = numReunion, Hippodrome = hippodrome, NumCourse = numCourse, IdCourse = idCourse, AgeMoyen = ageMoyen, Distance = distance, MaBase = maBase, Ch1 = ch1, Ch2 = ch2, Ch3 = ch3, Ch4 = ch4, Ch5 = ch5, PMaBase = pMaBase, PCh1 = pCh1, PCh2 = pCh2, PCh3 = pCh3, PCh4 = pCh4, PCh5 = pCh5, FlgFin = flgFin }); StatusMessage = $"{result} pronostics ajouté(s) {dateCourse}-{numReunion}-{hippodrome}-{numCourse}."; } catch (Exception ex) { StatusMessage = $"Impossible d'ajouter un pronostic : {dateCourse}-{numReunion}-{hippodrome}-{numCourse}.\n Erreur : {ex.Message}"; } } } }
ex :
J'ai placé genturfevo.db à la racine des projets GenTurfEvo et GenTurfEvo.Android avec propriété "Copier dans le répertoire de sortie = Toujours copier"
Elle apparait bien dans les deux répertoires suivants :
-GenTurfEvo\GenTurfEvo\bin\Debug
-GenTurfEvo\GenTurfEvo.Android\bin\Debug
Ensuite, j'ai déplacé la BDD dans le projet GenTurfEvo.Android sous l'arborescence Asset avec les propriétés suivantes :
Action de génération : AndroidAsset
Copier dans le répertoire : Toujours copier
Mais toujours rien...
Un autre tuto :https://www.it-swarm-fr.com/fr/andro...in/1042437841/
M'indique de copier le code suivant pour décompresser la BDD hors de l'apk :
Mais je ne sais pas où le placer !
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 string dbName = "db.sqlite"; string dbPath = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.ToString (), dbName); // Check if your DB has already been extracted. if (!File.Exists(dbPath)) { using (BinaryReader br = new BinaryReader(Android.App.Application.Context.Assets.Open(dbName))) { using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create))) { byte[] buffer = new byte[2048]; int len = 0; while ((len = br.Read(buffer, 0, buffer.Length)) > 0) { bw.Write (buffer, 0, len); } } } }
Merci de votre aide, Tchicken.
Partager