Пример #1
0
 void ExecSql(string statement, string table, bool isCreate)
 {
     if (NumberOfThreads > 1)
     {
         Monitor.Enter(Locker);
         try
         {
             if (isCreate)
             {
                 tables.Add(table);
             }
             Sqlite3.sqlite3_exec(pDb, statement, 0, 0, 0);
             rc = Sqlite3.sqlite3_errcode(pDb);
             //Monitor.Pulse(Locker);
         }
         finally
         {
             Monitor.Exit(Locker);
             //Debug.Print(String.Format("Section {0} Thread {1} released Locker", table, Thread.CurrentThread.ManagedThreadId));
         }
     }
     else
     {
         if (isCreate)
         {
             tables.Add(table);
         }
         Sqlite3.sqlite3_exec(pDb, statement, 0, 0, 0);
         rc = Sqlite3.sqlite3_errcode(pDb);
     }
 }
Пример #2
0
 /*
 ** Initialize the state information in data
 */
 public void main_init()
 {
     data = new callback_data(); //memset(data, 0, sizeof(*data));
     //data.mode = MODE_List;
     data.separator  = "|";      //memcpy(data.separator, "|", 2);
     data.showHeader = false;
     Sqlite3.sqlite3_initialize();
     Sqlite3.sqlite3_config(Sqlite3.SQLITE_CONFIG_URI, 1);
     Sqlite3.sqlite3_config(Sqlite3.SQLITE_CONFIG_LOG, new object[] { (Sqlite3.dxLog)shellLog, data, null });
     Sqlite3.sqlite3_config(Sqlite3.SQLITE_CONFIG_SERIALIZED);
     AttachDB(dbToMergeWith);
 }
Пример #3
0
        public void AttachDB(string filename)
        {
            int rc = 0;

            pDb = null;
            try
            {
                FileInfo fi = new FileInfo(filename);
                if (fi.Exists)
                {
                    //fi.Delete();
                }
            }
            catch (Exception ex)
            {
            }
            rc = Sqlite3.sqlite3_open(filename, out pDb);
        }
Пример #4
0
        public void DiskMerge()
        {
            string strQueryFront = "ATTACH database '" + dbToMergeWith + "' AS disk;";

            Sqlite3.sqlite3_exec(pDb, strQueryFront, 0, 0, 0);
            rc = Sqlite3.sqlite3_errcode(pDb);
            if (rc == 0)
            {
                foreach (string table in tables)
                {
                    Sqlite3.sqlite3_exec(pDb, "CREATE TABLE disk." + table + " AS SELECT * FROM " + table + ";", 0, 0, 0);
                    rc = Sqlite3.sqlite3_errcode(pDb);
                    if (rc != 0)
                    {
                        Sqlite3.sqlite3_close(pDb);
                        return;
                    }
                }
                rc = Sqlite3.exec(pDb, "DETACH disk;", 0, 0, 0);
            }
            Sqlite3.sqlite3_close(pDb);
        }
Пример #5
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);
        }
Пример #6
0
        /// <summary>
        /// Retrieves the name of all the tables within the DB
        /// </summary>
        /// <param name="db">pointer to database</param>
        /// <param name="zSelect"></param>
        /// <returns>List of tablenames in database</returns>
        List <string> run_table_names_query(sqlite3 db)
        {
            List <string> tableNames = new List <string>();
            string        getTables  = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";

            sqlite3_stmt pSelect = new sqlite3_stmt();
            int          rc;
            string       sDummy = null;

            rc = Sqlite3.sqlite3_prepare(db, getTables, -1, ref pSelect, ref sDummy);
            if (rc != Sqlite3.SQLITE_OK || null == pSelect)
            {
                return(tableNames);
            }
            rc = Sqlite3.sqlite3_step(pSelect);
            while (rc == Sqlite3.SQLITE_ROW)
            {
                tableNames.Add(Sqlite3.sqlite3_column_text(pSelect, 0));

                rc = Sqlite3.sqlite3_step(pSelect);
            }
            Sqlite3.sqlite3_finalize(pSelect);
            return(tableNames);
        }
Пример #7
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);
        }