the problem is I also need the field name for the Key.
Ok, I may have missed the goal here. I had assumed the main goal is to get the data types.
Since it seems (more clear to me) that the goal here is to not have to type in all those field names by hand?
You can use this approach:
eg:
Dim MachineData As Map
MachineData.Initialize
Dim strF As String
Dim Crsr As ResultSet = Main.sql.ExecQuery("select * from tblHotels")
Dim i As Int
Do While Crsr.NextRow
For i = 0 To Crsr.ColumnCount - 1
strF = Crsr.GetColumnName(i)
MachineData.Put(strF,Crsr.GetString(strF))
Next
Loop
Crsr.Close
So, the above is certainly a possible. You thus do NOT have to “hand code” the column names with above.
Quite sure the above was your "actual" question here. So, yes, you can get/grab/use the field name as per above.
The ONLY downside to above is that we pulling number types as string, and also pulling null values as a string. As I STRONG pointed out, you can risk this.
So, it not clear if the goal is just above, or we STILL want the strong data typing? (this is where I may have assumed wrong - my bad).
However, your posted code DOES have strong types - not just getstring.
So, if you REALLY need the data types?
That’s where my previous field type map code would be used. And VERY nice is that you could add a PK Boolean flag also (but lets take this one step at a time!!!).
So, one could make two geneal routines. (one to get the field data types), and another to return any sql as a map.
So, right now, the above loop may well suffice, but if you want the Map to be strong data typed to the column(s) data type?
However, if you want data typed maps?
Then the above loop wold become this:
Dim tblHotels As Map = GetTAbleDef("tblHotels")
Do While Crsr.NextRow
For i = 0 To Crsr.ColumnCount - 1
strF = Crsr.GetColumnName(i)
strType = tblHotels.Get(strF)
If strType.Contains("NVARCHAR") Then
MachineData.Put(strF,Crsr.GetString(strF))
Else if strType.Contains("INTEGER") Then
MachineData.Put(strF,Crsr.GetInt(strF))
Else if strType.Contains("NUMERIC") Then
MachineData.Put(strF,Crsr.GetDouble(strF))
End If
Next
Loop
Crsr.Close
Not too messay!
So you can use a “clean” loop to pull the resultset into a map. However, if you need/want to preserve the type (as opposed to just puling as string), then you have to adopt the above bit of code. It certainly a “bit more” messy, but this again could be a generalized bit of code that you pass the sql and it returns a map.
The “GetTableDef” routine is previous posted above, but as a separate
routine it could/would be:
Sub GetTableDef (strTAble As String) As Map
Dim rs As ResultSet = Main.sql.ExecQuery("PRAGMA table_info(" & strTAble & "customers);")
Dim MyFields As Map
MyFields.Initialize
Do While rs.NextRow
MyFields.Put(rs.GetString("name"), rs.GetString("type"))
Loop
rs.Close
Return MyFields
So, if you can live with “strings”, then you can loop to a map rather easy. If you “need” the strong data typing, then you have to add the above if/else set of statements to determine the data type. Either way, you can eliminate the need to “hand code” each row, and having to hand code the field names.
I actually miss-read the post. I thought the main goal was to get the data types. In reading again, the goal was not having to type by hand all the columns (hard code, and hand code,and presurabley save world poverty here).
However, since your posted code DID in fact have strong typed data, I assumed this was still part of the goal.
Regards,
Albert D. Kallal
Edmonton, Alberta Canada