B4X:
'Activity module
Sub Process_Globals
Dim SQL As SQL
End Sub
Sub Globals
Dim Btn_SetScore As Button
Dim EditText1 As EditText
Dim lbl_BirthDay As Label
Dim lbl_FailedTest As Label
Dim lbl_FirstName As Label
Dim lbl_FirstNameV As Label
Dim lbl_LastNameV As Label
Dim Lbl_Score As Label
Dim lbl_StudentID As Label
Dim lbl_Test As Label
Dim ListView1 As ListView
Dim Spn_TestID As Spinner
Dim StudentID_Spinner As Spinner
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Try
SQL.Initialize(File.DirDefaultExternal,"1.db",True)
Catch
Log(LastException.Message)
SQL.Initialize(File.DirInternal,"1.db",True)
End Try
End If
Activity.LoadLayout("1")
'Delete Old Tables
DBUtils.DropTable(SQL,"Students")
DBUtils.DropTable(SQL,"Grades")
DBUtils.DropTable(SQL,"DBVersion")
'Create the Student Table
Dim m As Map
m.Initialize
m.Put("Id",DBUtils.DB_TEXT)
m.Put("First Name",DBUtils.DB_TEXT)
m.Put("Last Name",DBUtils.DB_TEXT)
m.Put("BirthDay",DBUtils.DB_INTEGER)
DBUtils.CreateTable(SQL,"Students",m,"Id")
'How to use the DBVersion Subs
Dim DBVersion,CurrentDBVersion As Int
DBVersion = DBUtils.GetDBVersion(SQL)
CurrentDBVersion = 2
Do While DBVersion < CurrentDBVersion
Select DBVersion
Case 1
UpdateDB1_2(SQL)
End Select
DBVersion = DBUtils.GetDBVersion(SQL)
Loop
FillStudentsTable
FillGradesTable
DBUtils.ExecuteSpinner(SQL,"SELECT Id FROM Students",Null,0,StudentID_Spinner)
StudentID_Spinner_ItemClick(0,StudentID_Spinner.GetItem(0))
End Sub
Sub StudentID_Spinner_ItemClick (Position As Int, Value As Object)
Dim m As Map
m = DBUtils.ExecuteMap(SQL,"SELECT Id,[First Name],[Last Name],BirthDay FROM Students Where Id = ?",Array As String(Value))
If m = Null Then
lbl_StudentID.Text = "N/A"
lbl_BirthDay.Text = " "
Else
lbl_StudentID.Text = m.Get("First Name") & " " & m.Get("Last Name")
lbl_BirthDay.Text = DateTime.Date(m.Get("birthDay"))
End If
'Get the test for this specific Student
DBUtils.ExecuteSpinner(SQL,"SELECT test From Grade WHERE id = ?",Array As String(Value),0,Spn_TestID)
Spn_TestID.SelectedIndex = 0
Spn_TestID_ItemClick(0,Spn_TestID.GetItem(0))
End Sub
Sub Spn_TestID_ItemClick (Position As Int, Value As Object)
Dim m As Map
m = DBUtils.ExecuteMap(SQL,"SELECT Grade From Grade where Id = ? AND test = ? ",Array As String(Spn_TestID.SelectedItem,Value))
If m = Null Then
lbl_Test.Text = "N/A"
Else
lbl_Test.Text = m.Get("Grade")
End If
End Sub
Sub Btn_SetScore_Click
End Sub
Sub UpdateDB1_2(sq As SQL)
Dim m As Map
m.initialize
m.Put("Id",DBUtils.DB_TEXT)
m.Put("Test",DBUtils.DB_TEXT)
m.Put("Grade",DBUtils.DB_INTEGER)
DBUtils.CreateTable(sq,"Grade",m,"") 'no primary key assigned
DBUtils.SetDBVersion(sq,2)
End Sub
Sub FillStudentsTable
Dim ListOfMaps As List
ListOfMaps.Initialize
Dim Id As Int
For i = 1 To 40
Dim m As Map
m.initialize
Id = Rnd(Id + 1,Id + 10000)
m.Put("Id",NumberFormat2(Id, 6, 0, 0 , False))
m.Put("First Name","John")
m.Put("Last Name","Smith" & i)
m.Put("Birthday", DateTime.Add(DateTime.Now, Rnd(0,100), Rnd(0,12), Rnd(0, 30)))
ListOfMaps.Add(m)
Next
DBUtils.InsertMaps(SQL,"Students",ListOfMaps)
End Sub
Sub FillGradesTable
Dim Table As List
Table = DBUtils.ExecuteMemoryTable(SQL,"SELECT Id FROM Students",Null,0)
Dim Cols() As String
Dim ListOfMaps As List
ListOfMaps.Initialize
For Test = 1 To 20
For Student = 0 To Table.Size - 1
Dim m As Map
m.initialize
Cols = Table.Get(Student)
m.put("Id",Cols(0))
m.put("Test","Test" & Test )
m.put("Grade",Rnd(0,101))
ListOfMaps.Add(m)
Next
Next
DBUtils.InsertMaps(SQL,"Grade",ListOfMaps)
End Sub
I am getting a NumberFormatException in above code at -
B4X:
lbl_BirthDay.Text = DateTime.Date(m.Get("birthDay"))
I really dont understand,why I am getting this exception,I cross referenced it several times with original DBUtils example code,but still i can't figure it out :sign0085: