Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQLite3 program getting name of table

SQLite3 program getting name of table

Scheduled Pinned Locked Moved Database
databasesqlitecsharphtmldebugging
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bbranded
    wrote on last edited by
    #1

    Hello, Following the faq, I am attempting to query the a DB to obtain the list of table names with the following query: SELECT * FROM sqlite_master WHERE type='table' ORDER BY name I am using an ODBC driver and have written code in c#:

    DbConnection.Open();

    OdbcCommand DbCommand = DbConnection.CreateCommand();

    DbCommand.CommandText = ("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name");

    DbReader = DbCommand.ExecuteReader();

    List TableNames = new List();

    for (int i = 0; i < DbReader.FieldCount; i++)
    {
    TableNames.Add(DbReader.GetName(i));
    Trace.WriteLine(DbReader.GetName(i));
    }

    DbReader.Close();
    DbConnection.Close();

    I have a feeling that dbreader.getname(i) is returning the wrong thing, versus the query being incorrect. Can anyone help me figure out a way to identify the names of tables in an SQLite3 DB? Thanks, Matt

    L 1 Reply Last reply
    0
    • B bbranded

      Hello, Following the faq, I am attempting to query the a DB to obtain the list of table names with the following query: SELECT * FROM sqlite_master WHERE type='table' ORDER BY name I am using an ODBC driver and have written code in c#:

      DbConnection.Open();

      OdbcCommand DbCommand = DbConnection.CreateCommand();

      DbCommand.CommandText = ("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name");

      DbReader = DbCommand.ExecuteReader();

      List TableNames = new List();

      for (int i = 0; i < DbReader.FieldCount; i++)
      {
      TableNames.Add(DbReader.GetName(i));
      Trace.WriteLine(DbReader.GetName(i));
      }

      DbReader.Close();
      DbConnection.Close();

      I have a feeling that dbreader.getname(i) is returning the wrong thing, versus the query being incorrect. Can anyone help me figure out a way to identify the names of tables in an SQLite3 DB? Thanks, Matt

      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      The DbReader you obtained can be used to retrieve the table names from the "name" field: DbReader["name"] So your program should be modified to be like this:

      DbConnection.Open();

      OdbcCommand DbCommand = DbConnection.CreateCommand();

      DbCommand.CommandText = ("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name");

      DbReader = DbCommand.ExecuteReader();

      List TableNames = new List();

      while(DbReader.Read()){
      TableNames.Add(DbReader["name"]);
      Trace.WriteLine(DbReader["name"]);
      }

      DbReader.Close();
      DbConnection.Close();

      Your original code was trying to retrieve all the fields in one record. Good luck!

      B 1 Reply Last reply
      0
      • L loyal ginger

        The DbReader you obtained can be used to retrieve the table names from the "name" field: DbReader["name"] So your program should be modified to be like this:

        DbConnection.Open();

        OdbcCommand DbCommand = DbConnection.CreateCommand();

        DbCommand.CommandText = ("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name");

        DbReader = DbCommand.ExecuteReader();

        List TableNames = new List();

        while(DbReader.Read()){
        TableNames.Add(DbReader["name"]);
        Trace.WriteLine(DbReader["name"]);
        }

        DbReader.Close();
        DbConnection.Close();

        Your original code was trying to retrieve all the fields in one record. Good luck!

        B Offline
        B Offline
        bbranded
        wrote on last edited by
        #3

        Thanks for the modified code! Can't make a list like that for some reason. Maybe I'm missing an import; I kept the casting. Because of this:

                OdbcCommand DbCommand = DbConnection.CreateCommand();
                
                //SQL query to return all table names
                //http://www.sqlite.org/faq.html#q7
                DbCommand.CommandText = ("SELECT name FROM sqlite\_master WHERE type='table' ORDER BY name");
        
                DbReader = DbCommand.ExecuteReader();
                
        
                List<string> TableNames = new List<string>();
        
                while (DbReader.Read())
                {
                    TableNames.Add(DbReader\["name"\].ToString());
                    Trace.WriteLine(DbReader\["name"\]);
                }
        
        
                DbReader.Close();
        
                if (TableNames.Contains("bandwidth\_records"))
                {
                    //check to make sure column names are present
                    DbConnection.Close(); 
                }
                else
                {
                    //create table & columns
                )
        

        Thanks!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups