Autore Topic: SQLite: come avere i nomi dei campi con nomi sconosciuti ?  (Letto 711 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.316
  • Ne mors quidem nos iunget
    • Mostra profilo
SQLite: come avere i nomi dei campi con nomi sconosciuti ?
« il: 23 Novembre 2013, 21:12:11 »
Riporto di seguito questa discussione apparsa nella M.L. ufficiale:


" I am trying to get a list of the tables in a SQLite database.

According to SQLite.org the table called "sqlite_master" lists all the tables and indices in the DB[1]. I have a SQlite browser tool that seems to confirm that, because it returns table names from the target DB, when this is executed: SELECT 'tables' from SQLITE_MASTER

My problem is getting the table names and the fields in each of the tables of a Database with an unknown structure. For this project, I do not know the name of the fields, because I am looking at DB's created by someone else. That means that the example included in the definition of "For Each" will not work.


Using Gambas, I connect to the SQlite database, then do a query:

Codice: gambas [Seleziona]

Public Sub DisplayDbInfo()
   
    Dim strDbTablesQuery As String    ' The string that is the select statement
    Dim rsDbTableInfo As Result

    strDbTablesQuery = "Select name From sqlite_master WHERE type = 'table' ORDER BY name"

    rsDbTableInfo = $hConn.Exec(strDbTablesQuery)
    ' For debugging:
    Print "Num of Tables = " & rsDbTableInfo.Count     ' 9 Tables
    Print "rsDbTableInfo.max = " & rsDbTableInfo.Max   ' 8
    Print "Num of Fields returned = " & rsDbTableInfo.Fields.Count
   
    For intCount = 0 To rsDbTableInfo.Fields.Count - 1  ' Number of Fields found
         Print "rsDbTableInfo.Fields[i] = " & Str(rsDbTableInfo.Fields[intCount])
    Next


However, all I get from the previous lines is this:  (ResultField 0x1aa6718).
     
    I tried using 'Result', 'ResultField', 'Result.Felds' and other things, but I cannot get field names.
     
    How do I get the actual name of the field?
   
paul
"


" Salut Paul,

this is out from DB-diff3 (you can get the whole code from here : http://dashboard68.users.sourceforge.net/  )

Codice: gambas [Seleziona]
Private Function fill_Fields(tbl As Table) As Collection
Dim fld As Field
Dim sKeyPraefix As String = tbl.Name & sSep & "FIELDS" & sSep
Dim colFields As New Collection
Dim clsFld As ClsFields
Dim sPKey As String
   For Each fld In tbl.Fields
      sPKey = Upper(sKeyPraefix & fld.Name)
      clsFld = New ClsFields
      clsFld.Key = sPKey
      clsFld.fldDefault = fld.Default
      clsFld.fldLength = fld.Length
      clsFld.fldName = fld.Name
      clsFld.fldTable = fld.Table
      clsFld.fldType = fld.Type
      colFields.Add(clsFld, sPKey)
   Next
   Return colFields
End


--
Amicalement
Charlie
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline naderit

  • Grande Gambero
  • ***
  • Post: 140
    • Mostra profilo
Re: SQLite: come avere i nomi dei campi con nomi sconosciuti ?
« Risposta #1 il: 27 Maggio 2015, 22:06:42 »
Ciao anche se la discussione è un po' datata, vorrei condividere questa soluzione all'elenco dei nomi delle tabelle di un db sqlite.

Dim count_tab As Result
Dim sql As String
Dim i As Integer

Mod_Conn.db_prova ' connessione al db
 
 sql = "SELECT name FROM sqlite_master WHERE type = 'table'"   'è possibile ottenere l'accesso a nomi di tabella e indice facendo un SELECT su una tabella speciale denominata "sqlite_master"
 count_tab = Mod_Conn.$conn.Exec(sql, Null)

For i = 0 To count_tab.Max ' ciclo per scrivere i nomi delle tabelle nella listbox

  count_tab.MoveTo(i)
  ListBox1.Add(count_tab[0])
Next

Mod_Conn.$conn.Close