工程源码已上传。
类DBUtils
布局文件
运行截图
运行截图
main模块源码
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: DBUtils
#VersionCode: 2
#VersionName: 1.01
#SupportedOrientations: portrait
#End Region
Sub Process_Globals
End Sub
Sub Globals
Private lblStudentName As Label
Private spnrStudentId As Spinner
Private spnrTests As Spinner
Private lblBirthday As Label
Private lstFailedTest As ListView
Private txtGrade As EditText
Private WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
'Fill the students id spinner.
DBUtils.ExecuteSpinner(Starter.SQL, "SELECT Id FROM Students", Null, 0, spnrStudentId)
spnrStudentId_ItemClick(0, spnrStudentId.GetItem(0))
End Sub
Sub ShowTableInWebView 'ignore
WebView1.Visible = True
'[First Name] || ' ' || [Last Name] As Name =>
'Creates a column named Name which is made of the First Name AND Last Name fields (with a space between them).
'date(birthday / 1000, 'unixepoch', 'localtime') =>
'Create a column named Birthday. This method converts the stored B4A time (milliseconds since 1/1/1970) to a string.
WebView1.LoadHtml(DBUtils.ExecuteHtml(Starter.SQL, _
"SELECT Id, [First Name] || ' ' || [Last Name] As Name, date(birthday / 1000, 'unixepoch', 'localtime') As Birthday FROM Students" _
, Null, 0, True))
End Sub
Sub WebView1_OverrideUrl (Url As String) As Boolean
'parse the row and column numbers from the URL
Dim values() As String
values = Regex.Split("[.]", Url.SubString(7))
Dim col, row As Int
col = values(0)
row = values(1)
ToastMessageShow("User pressed on column: " & col & " and row: " & row, False)
Return True 'Don't try to navigate to this URL
End Sub
Sub ExportToJSON 'ignore
Dim gen As JSONGenerator
gen.Initialize(DBUtils.ExecuteJSON(Starter.SQL, "SELECT Id, [Last Name], Birthday FROM Students", Null, _
0, Array As String(DBUtils.DB_TEXT, DBUtils.DB_TEXT, DBUtils.DB_INTEGER)))
Dim JSONString As String
JSONString = gen.ToPrettyString(4)
Msgbox(JSONString, "")
End Sub
Sub spnrStudentId_ItemClick (Position As Int, Value As Object)
Dim m As Map
m = DBUtils.ExecuteMap(Starter.SQL, "SELECT Id, [First Name], [Last Name], Birthday FROM students WHERE id = ?", _
Array As String(Value))
If m = Null Or m.IsInitialized = False Then 'Null will return if there is no match
lblStudentName.Text = "N/A"
lblBirthday.Text = ""
Else
lblStudentName.Text = m.Get("first name") & " " & m.Get("last name") 'keys are lower cased!
lblBirthday.Text = DateTime.Date(m.Get("birthday"))
End If
'Get the tests for this specific student (currently it is all tests).
DBUtils.ExecuteSpinner(Starter.SQL, "SELECT test FROM Grades WHERE id = ?", _
Array As String(Value), 0, spnrTests)
spnrTests.SelectedIndex = 0
spnrTests_ItemClick(0, spnrTests.GetItem(0))
FindFailedTests(Value)
End Sub
Sub spnrTests_ItemClick (Position As Int, Value As Object)
'Show the grade of this test
Dim m As Map = DBUtils.ExecuteMap(Starter.SQL, "SELECT Grade FROM Grades WHERE id = ? AND test = ?"