Créer une table SQLite pour stocker des données hétérogénes.
Bonjour à tous,
je me pose la question du choix du format de données pour stocker des données de mon programme dans une table SQLite que j'appelle "Settings" et possédant 2 colonnes "Key" et "Value".
"Key" sera toujours du string mais "Value" pourra être du Boolean, du String, de l'Integer....Vous voyez l'idée.
J'hésite à utiliser le format TEXT ou BLOB.....j'aimerais bien votre avis.
Pour le BLOB j'ai un exemple ci-dessous pour lire les données mais je ne sais pas les écrires par contre...
Ca serait très sympa si je pouvais avoir un peu d'aide la dessus car je bloque.
Code:
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
| Public Function LoadDataLocalStorage(FileName As String) As DataTable
Dim dt As New DataTable()
Dim query As String = "select * FROM Settings"
Dim NewTable As New DataTable(("Settings"))
NewTable.Columns.Add("Key", GetType(String))
NewTable.Columns.Add("Value", GetType(String))
Try
Dim cnn As New SQLiteConnection(String.Format("Data Source={0}", FileName ))
cnn.Open()
Dim mycommand As New SQLiteCommand(cnn)
mycommand.CommandText = query
Dim reader As SQLiteDataReader = mycommand.ExecuteReader()
dt.Load(reader)
reader.Close()
cnn.Close()
Catch e As Exception
MessageBox.Show(String.Format("{0}." & Environment.NewLine & "le fichier '{1}' n'existe pas ou est invalide, vérifier l'installation du logiciel.", e.Message, FileName), "Erreur d'ouverture du fichier LocalStorage", MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
Return Nothing
End Try
For Each OldRow As DataRow In dt.Rows
Dim NewRow As DataRow = NewTable.NewRow
Dim Key As String
Dim Value As String
If Not IsDBNull(OldRow("Key")) Then
Key = CStr(OldRow("Key"))
Else
Key = Nothing
End If
If Not IsDBNull(OldRow("Value")) Then
Value = Text.Encoding.Unicode.GetString(CType(OldRow("Value"), Byte()))
Else
Value = Nothing
End If
NewRow("Key") = Key
NewRow("Value") = Value
NewTable.Rows.Add(NewRow)
If Key = "player.nameFirst" Then GameSettings.PlayerNameFirst = Value
If Key = "player.nameLast" Then GameSettings.PlayerNameLast = Value
If Key = "player.nameNick" Then GameSettings.PlayerNameNick = Value
If Key = "player.country" Then GameSettings.PlayerCountry = Value
If Key = "player.countryCode" Then GameSettings.PlayerCountryCode = Value
Next
Return NewTable
End Function |
Merci beaucoup à vous.