Example #1
0
        List <string> run_column_name_query(sqlite3 db, string table)
        {
            List <string> columnNames = new List <string>();
            string        sql         = "PRAGMA table_info(" + table + ");";
            sqlite3_stmt  pSelect     = new sqlite3_stmt();
            int           rc;
            string        sDummy = null;

            rc = Sqlite3.sqlite3_prepare(db, sql, -1, ref pSelect, ref sDummy);
            if (rc != Sqlite3.SQLITE_OK || null == pSelect)
            {
                return(columnNames);
            }
            rc = Sqlite3.sqlite3_step(pSelect);
            while (rc == Sqlite3.SQLITE_ROW)
            {
                sqlite3_value val = Sqlite3.sqlite3_column_value(pSelect, 1);
                columnNames.Add(val.z);
                rc = Sqlite3.sqlite3_step(pSelect);
            }
            Sqlite3.sqlite3_finalize(pSelect);
            return(columnNames);
        }
Example #2
0
        public DataSet GetDBTables()
        {
            DataSet       fredDs = new DataSet("FRED");
            List <string> tables = run_table_names_query(pDb);
            int           rc     = 0;

            foreach (string table in tables)
            {
                bool          firstRun = true;
                List <string> names    = new List <string>();
                List <string> arrRows  = new List <string>();
                List <string> colNames = run_column_name_query(pDb, table);
                string        zSql     = "SELECT * from " + table + ";";
                sqlite3_stmt  pSelect  = new sqlite3_stmt();
                string        sDummy   = null;
                rc = Sqlite3.sqlite3_prepare(pDb, zSql, -1, ref pSelect, ref sDummy);
                if (rc != Sqlite3.SQLITE_OK || null == pSelect)
                {
                    continue;
                }
                rc = Sqlite3.sqlite3_step(pSelect);
                DataTable dt = new DataTable(table);
                while (rc == Sqlite3.SQLITE_ROW)
                {
                    //Get the data in the cell
                    int     total = Sqlite3.sqlite3_column_count(pSelect);
                    DataRow dr    = dt.NewRow();

                    List <string> values = new List <string>();
                    for (int i = 0; i < total; i++)
                    {
                        int           valInt = 0;
                        string        value  = "";
                        sqlite3_value val    = Sqlite3.sqlite3_column_value(pSelect, i);
                        if (val.type == 1)
                        {
                            valInt = Sqlite3.sqlite3_column_int(pSelect, i);
                            value  = valInt.ToString();
                        }
                        else
                        {
                            value = val.z;
                        }
                        values.Add(value);
                        if (firstRun)
                        {
                            string name = Sqlite3.sqlite3_column_name(pSelect, i);
                            if (name == null)
                            {
                                continue;
                            }
                            names.Add(name);
                            dt.Columns.Add(name);
                        }
                    }
                    rc       = Sqlite3.sqlite3_step(pSelect);
                    firstRun = false;
                    for (int i = 0; i < names.Count && i < values.Count; i++)
                    {
                        dr[names[i]] = values[i];
                    }
                    dt.Rows.Add(dr);
                }
                Sqlite3.sqlite3_finalize(pSelect);
                fredDs.Tables.Add(dt);
            }
            Sqlite3.sqlite3_close(pDb);
            return(fredDs);
        }