Se ti garba, puoi passare al nostro amico il codice seguente.
Questo potrebbe essere un esercizio utile a comprendere le varie fasi che vengono scritte in console.
Inoltre nella cartella Database all'interno del programma troverà il databasetest
Potrà vederlo all'interno della IDE facendo click col tasto destro del mouse su Connessioni ... Nuova connessione dalla finestra sceglie:
Il codice seguente va copiato e incollato in un modulo (classe di avvio) di un nuovo progetto database:
Private $hConn As New Connection
Private $sNameDB As String = "databasetest"
Private $sPathDB As String = Application.Path &/ "Database"
Public Sub Main()
CheckDB
End
Private Sub CheckDB()
db.Debug = True
If Not Exist($sPathDB) Then Try Mkdir $sPathDB
With $hConn
.Close()
.Type = "sqlite3"
.Host = $sPathDB
.Open()
If Not $hConn.Databases.Exist($sNameDB) Then
.Databases.Add($sNameDB)
.Close()
.Name = $sNameDB
.Open()
.Begin()
MakeTable()
FillTable()
.Commit()
Print ("#Database successfully created!")
Else
Print ("#Existing database")
Endif
.Close()
End With
Catch
$hConn.Rollback()
Error.Raise(Error.Text & " - " & Error.Code)
End
Private Sub MakeTable()
Dim hTable As Table
' Create the Addresses table
hTable = $hConn.Tables.Add("taddr")
hTable.Fields.Add("adkey", db.Serial)
hTable.Fields.Add("adnam", db.String, 32)
hTable.Fields.Add("adsur", db.String, 32)
hTable.Fields.Add("adfem", db.Boolean)
hTable.Fields.Add("adadd", db.String, 64)
hTable.Fields.Add("adzip", db.String, 8)
hTable.Fields.Add("adcit", db.String, 32)
hTable.Fields.Add("adpro", db.String, 32)
hTable.Fields.Add("adnot", db.String,, "No note")
hTable.PrimaryKey = ["adkey"]
hTable.Update
End
Private Sub FillTable()
' Populate the address table with test records.
' I apologize for the low adherence to reality.
Dim i As Integer
Dim aStreet As String[] = ["St. ", "Ln. ", "Ave. ", "Blvd."]
Dim aZciPro As String[]
Dim aFirstName As String[] = ["Binah", "Hanna", "Selassie", "Okpara", "Agustina", "Clema", "Coyan", "Eusebio", "Diego", "Alisha", "Daphne", "Wiley", "Valentine", "Lidwina", "Roxana", "Edmund", "Hildeger", "Adrianne", "Janette", "Quentin", "Sylvain", "Francesco", "Marco", "Aldina", "Ornella", "Carmencita", "Galena", "Florentino", "Rodrigo", "Benoit", "Mario"]
Dim aLastName As String[] = ["Smith", "Johnson", "Williams", "Brown", "Martin", "Bernard", "Dubois", "Thomas", "Robert", "Richard", "Müller", "Schmidt", "Schneider", "Fischer", "Weber", "Meyer", "Gunnarsson", "García", "González", "Rodríguez", "Fernández", "López", "Martínez", "Jensen", "Nielsen", "Cohen", "Friedman", "Rossi", "Bianchi", "Minisini", "Verdone"]
Dim hResult As Result
For i = 0 To 99
aFirstName.Shuffle()
aLastName.Shuffle()
aStreet.Shuffle()
hResult = $hConn.Create("taddr")
hResult!adnam = aFirstName[0]
hResult!adsur = aLastName[0]
hResult!adfem = Female(aFirstName[0])
hResult!adadd = aStreet[0] & " " & aLastName[2] & " n. " & (i + 10)
aZciPro = Zcipro()
hResult!adzip = aZciPro[2]
hResult!adcit = aZciPro[0]
hResult!adpro = aZciPro[1]
hResult.Update
Next
End
Private Function Female(name As String) As Boolean
' Returns if female is true or false
If name Ends "a" Then
If name <> "Okpara" Then
Return True
Endif
Else If name Ends "e" Then
If name <> "Selassie" Then
Return True
Endif
Endif
Return False
End
Private Function Zcipro() As String[]
' Returns in vectors form: Zip, City, Country in Italian style ;-)
Dim ss As New String[]
Dim n As Integer[] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n.Shuffle()
Select n[0]
Case 1
ss.Push("New York")
ss.Push("New York")
ss.Push("10100")
Case 2
ss.Push("Los Angeles")
ss.Push("California")
ss.Push("32000")
Case 3
ss.Push("Chicago")
ss.Push("Illinois")
ss.Push("70100")
Case 4
ss.Push("Houston")
ss.Push("Texas")
ss.Push("24000")
Case 5
ss.Push("Phoenix")
ss.Push("Arizzona")
ss.Push("40032")
Case 6
ss.Push("Philadelphia")
ss.Push("Pennsylvania")
ss.Push("22100")
Case 7
ss.Push("San Antonio")
ss.Push("Texas")
ss.Push("24100")
Case 8
ss.Push("Jacksonville")
ss.Push("Florida")
ss.Push("50100")
Case 9
ss.Push("Denver")
ss.Push("Colorado")
ss.Push("80000")
End Select
Return ss
End