This snippet gets all tables, column names and definitions (e.g. INTEGER, TEXT, etc.) and returns a list with maps.
It is good for:
- when you want a dynamic layout (e.g. ONE Activity to display & edit several tables) it is good to know all the parms before you display the data (like number of columns, columnnames, etc.)
- formatting the data you display (e.g. you want to format the column values it is good to know which format it has before you display it -> REAL values = 23.0 -> 23.00)
- when you insert/update a row you need to know the format
Call it:
It is good for:
- when you want a dynamic layout (e.g. ONE Activity to display & edit several tables) it is good to know all the parms before you display the data (like number of columns, columnnames, etc.)
- formatting the data you display (e.g. you want to format the column values it is good to know which format it has before you display it -> REAL values = 23.0 -> 23.00)
- when you insert/update a row you need to know the format
B4X:
Sub GetTablePragmas(DBDir As String, DBName As String) As List
Dim SQLP As SQL
#if B4J
SQLP.InitializeSQLite(DBDir, DBName , True)
#Else
SQLP.Initialize(DBDir, DBName , True)
#End If
Dim ReturnList As List
ReturnList.Initialize
Private DBQuery, TBQuery As String
Dim TBName, ColumnName, ColumnType As String
Log("DB-Name: " & DBName)
DBQuery = "Select name FROM sqlite_master WHERE Type='table' ORDER BY name"
Dim RSDB As ResultSet=SQLP.ExecQuery(DBQuery)
Do While RSDB.NextRow
TBName=RSDB.GetString2(0)
Log(" Table: " & TBName)
If TBName.StartsWith("SQlite") = False Then
Dim TBMap As Map
TBMap.Initialize
TBMap.Put("tbname",TBName)
TBQuery = "PRAGMA table_info(" & TBName & ")"
Dim TBRS As ResultSet=SQLP.ExecQuery(TBQuery)
Dim ColumnParmsMap As Map
ColumnParmsMap.Initialize
Do While TBRS.NextRow
For i=0 To TBRS.ColumnCount-1
Log (" " & i & ": " & TBRS.GetColumnName(i) & "->" & TBRS.GetString2(i))
ColumnParmsMap.Put(TBRS.GetColumnName(i), TBRS.GetString2(i))
Next
Loop
TBMap.Put("columnparms",ColumnParmsMap)
ReturnList.Add(TBMap)
TBRS.Close
End If
Loop
RSDB.Close
SQLP.Close
Return ReturnList
End Sub
Call it:
B4X:
Dim TableInfo As List
TableInfo.Initialize
TableInfo=GetTablePragmas(Dir,DbName)
Last edited: