in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Getting the DDL for a table

Last post 10-29-2006 12:59 PM by Rasha. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 10-29-2006 6:58 AM

    Getting the DDL for a table

    Hello,

    I can retrieve the SQL for a views via the following function:

    /// <summary>
    /// Gets the SQL executed by a given VIEW.
    /// </summary>
    /// <returns>
    /// The source of the given view.
    /// </returns>
    public virtual string GetViewSQL(string View) {
        DataTable dt;
        dt = Cn.GetSchema
            ("Views", new string[] {null, null, View});
        return (string) dt.Rows[0]["VIEW_DEFINITION"];
    }

    I would like to be able to do similar to get the DDL for a table. Is this possible?
  • 10-29-2006 8:16 AM In reply to

    • Rasha
    • Top 25 Contributor
    • Joined on 06-08-2006
    • Posts 35

    Re: Getting the DDL for a table

    Do not know if there is a more convenient way in the provider itself, but this will do the job:

    public virtual string GetTableSQL(string Table) {
        using (SQLiteCommand cmd = Cn.CreateCommand()) {
              cmd.CommandText =  "SELECT sql FROM sqlite_master " +
                                    "WHERE name = @tablename";
                cmd.Parameters.Add("@tablename", DbType.String);
                cmd.Parameters["@tablename"].Value = Table;
                 return cmd.ExecuteScalar();
        }
    }
  • 10-29-2006 9:20 AM In reply to

    Re: Getting the DDL for a table

    Raja,

    It worked with some modification to take into account the fact that I am doing some funky inheritance. The code is living in my MSAccess/SQLite database viewer available at: http://sourceforge.net/projects/plane-disaster/.
    Its in the SVN repo. I will probably release a new version soon.

    /// <summary>
            /// Gets the SQL executed by a given TABLE.
            /// </summary>
            /// <remarks>
            /// Posted by Rasha in http://sqlite.phxsoftware.com/forums/thread/2272.aspx
            /// </remarks>
            /// <returns>
            /// The DDL of the given table.
            /// </returns>
            public virtual string GetTableSQL(string Table) {
                using (SQLiteCommand cmd = (SQLiteCommand)Cn.CreateCommand()) {
                    cmd.CommandText =  "SELECT sql FROM sqlite_master " +
                        "WHERE name = @tablename";
                    cmd.Parameters.Add("@tablename", DbType.String);
                    cmd.Parameters["@tablename"].Value = Table;
                    return (string) cmd.ExecuteScalar();
                }
            }


  • 10-29-2006 12:59 PM In reply to

    • Rasha
    • Top 25 Contributor
    • Joined on 06-08-2006
    • Posts 35

    Re: Getting the DDL for a table

    Glad to see such project... and to be of any help. Will check it out.
    By the way, this would work for views also.
    Check out SELECT * FROM sqlite_master, and you will probably get some interesting ideas.
    Regards.


Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems