in

System.Data.SQLite

An open-source, enhanced version of the SQLite database engine for Windows

How do I get a list of Tables in a Database?

Last post 03-25-2008 6:15 PM by bfr. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 01-14-2008 2:20 PM

    How do I get a list of Tables in a Database?

    I've learned all the basics... but i'm stuck... i've been trying this for 2 hours and finally am forced to ask.

    Dim SchemaTable = SQLconnect.GetSchema(SQLiteMetaDataCollectionNames.Tables)
        For int As Integer = 0 To SchemaTable.Rows.Count - 1
        MsgBox(SchemaTable.Rows(int)!TABLE_TYPE.ToString)
        If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
            ListBox1.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
        End If
    Next

    I know this is completely wrong now... but how exactly do I get the list of tables. Answer in either C# or VB... i know both I'm just working in vb right now.

    Thank you so much :).

  • 01-14-2008 3:04 PM In reply to

    Re: How do I get a list of Tables in a Database?

    Try this:

     

     Dim seleccionSQL as string="SELECT name FROM SQLITE_MASTER where type='table'"

    Dim conexion As SQLite.SQLiteConnection 

    conexion = New SQLiteConnection _
                ("Data Source=" & strPathBD _
                & ";UseUTF16Encoding=False;Synchronous=Normal; Version=3;New=False")

    conexion.Open()

    Dim da As IDbDataAdapter = New SQLiteDataAdapter
    da.SelectCommand = New SQLiteCommand(seleccionSQL, conexion)
    Dim ds as new DataSet
    da.Fill(ds)
    conexion.Close()

     

    Then, use something like this to obtain every table name:

    ds.tables(0).rows(0).Item(0).tostring

    ds.tables(0).rows(1).Item(0).tostring

    ...and so on.

    Opto ut valeas, puerule. 

     

    Adde parvum parvo magnus acervus erit
  • 01-14-2008 4:36 PM In reply to

    Re: How do I get a list of Tables in a Database?

    You neglected to tell me how you're stuck, and what's wrong ... your call to GetSchema() looks fine.

    I don't know much about VB syntax, so your ! table access methods seem really funky and weird to me ... but without knowing why you're stuck, I can't really recommend the next step.

    And for the record -- apologies to Toni -- but don't use his suggestion :)  It's SQLite-specific and only works for certain kinds of schema queries.  GetSchema() is much more robust, somewhat DB-neutral, and can provide massively more detailed information at the column, index and foreign key levels.

    Robert

     

  • 01-14-2008 5:13 PM In reply to

    Re: How do I get a list of Tables in a Database?

    Thank you so much. Worked perfectly.

    Robert do you know the answer in C# then. I know both of them pretty well.

     

    And to tell you the truth I don't know what the ! means. I saw an example online and thought if the namespaces were simular it might work. Almost.

    I would love to have a system that was better standards but... if i can't find a way that works I just have to stick with what does.

    My example was a complete guess... so really if i can get a list of the tables in anyway it would be great (and once again my code probably doesn't make sense that i gave it was just a hack job that i kind of though made sense).

  • 01-14-2008 5:24 PM In reply to

    Re: How do I get a list of Tables in a Database?

    Easiest thing to do is just run this line in the debugger ... 

    Dim SchemaTable = SQLconnect.GetSchema(SQLiteMetaDataCollectionNames.Tables)

    Then view the SchemaTable variable in the DataTable viewer (the magnifying glass thing) in the debug window.  It'll grid-out the entire DataTable for you so you can see what the columns are in the data table.

     

  • 01-14-2008 6:23 PM In reply to

    Re: How do I get a list of Tables in a Database?

     Thanks robert... i forgot I used "table" not "TABLE"

     just took you saying look over it again for me to notice (and look back). And no i don't know how or why "!" does something. I'll figure out later.

    SUCH a SIMPLE fix 

    Dim SchemaTable = SQLconnect.GetSchema(SQLiteMetaDataCollectionNames.Tables)
            For int As Integer = 0 To SchemaTable.Rows.Count - 1
                If SchemaTable.Rows(int)!TABLE_TYPE.ToString() = "table" Then
                    ListBox1.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
                End If
            Next

  • 01-15-2008 2:26 AM In reply to

    Re: How do I get a list of Tables in a Database?

    You are right!  I will bear it in mind next time.

    Thanks 

    Adde parvum parvo magnus acervus erit
  • 03-12-2008 11:02 AM In reply to

    • new2that
    • Not Ranked
    • Joined on 03-12-2008
    • Nice/France/Europe/World
    • Posts 1

    Re: How do I get a list of Tables in a Database?

    Hi! Sorry, really starting with sql, sqlite, c#... So my question may be stupid:

    How to 'translate' the famous '!' in c# (If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "table")?

    Actually I can get the values using this strong (but uggly) code : 

    DataTable dt = conn.GetSchema(SQLiteMetaDataCollectionNames.Tables);

    for (int i=0; i<dt.Rows.Count;i++)

        if (dt.Rows[i].ItemArray[3 /* e.g. TABLE_TYPE */].ToString()  == "table")

            .../... 

     }

    Thanks

    Lo 

     

  • 03-24-2008 3:15 PM In reply to

    • undef
    • Top 500 Contributor
    • Joined on 03-19-2008
    • Posts 3

    Re: How do I get a list of Tables in a Database?

    Hi,

    I too am trying to get a list of tables in an SQLite DB, using C#. I've had a look around but can't find any example code for what I guess it a common task.

    Looking in the debugger I can see how the code provided by Lo above works, but as mentioned it doesn't seem very robust to be using array indices when these could change in future (ItemArray[3]). So what's the right way to do this?

    My current code:

    ---------------------- 

    DataTable dt = myConnection.GetSchema(SQLiteMetaDataCollectionNames.Tables);

    foreach (DataRow dr in dt.Rows)
    {
        /* find the table name in a robust way! */

    }

    ---------------------

    Thanks! 

  • 03-25-2008 6:15 PM In reply to

    • bfr
    • Top 50 Contributor
    • Joined on 01-19-2006
    • Posts 18

    Re: How do I get a list of Tables in a Database?

    Excuting this query will return the table names:

     SELECT name FROM sqlite_master WHERE type='table'

     See http://www.sqlite.org/faq.html#q7 for futher details.
    
Page 1 of 1 (10 items)
Powered by Community Server (Commercial Edition), by Telligent Systems