Jump to content
  • Advertisement
Sign in to follow this  
JPulham

[.net] Mono.Data.SqliteClient dies in the middle of a bulk insert

This topic is 3895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a function that inserts all of the values from a CSV file into a database. after 8 lines an Exception is thrown saying 'SQL Logic error or missing database' on the first column. It works fine until then????? why would this happen... the SQL generated looks normal. Could it be trying to insert loads of values maybe broke it? I am using Mono.Data.SqliteClient on .NET 2.0. heres the function:
    public bool ReadMeterCSV(string file, int pk)
    {
        IDbCommand ts_cmd;
        IDataReader ts_reader;
        string datestamp = "0.0";
        bool data_present;

        ts_cmd = _conn.CreateCommand();
        ts_cmd.CommandText = "SELECT timestamp FROM Curtail_Readings WHERE meterID = " + Convert.ToString(pk);
        ts_reader = ts_cmd.ExecuteReader();
        if(ts_reader.Read())data_present = true;
        else data_present = false;
        ts_cmd.Dispose();
        ts_cmd = null;

        if(data_present)
        {
            ts_cmd = _conn.CreateCommand();
            ts_cmd.CommandText = "SELECT MAX(timestamp) FROM Curtail_Readings WHERE meterID = " + Convert.ToString(pk);
            ts_reader = ts_cmd.ExecuteReader();
            if(ts_reader.Read())
                datestamp = ts_reader.GetString(0);
            ts_cmd.Dispose();
            ts_cmd = null;
        }
        string crash_str = "";
        int crash_i = 0;
        try
        {
            using(StreamReader sr = new StreamReader(file))
            {
                using(IDbTransaction trans = _conn.BeginTransaction())
                {
                    using(IDbCommand cmd = _conn.CreateCommand())
                    {
                        string line;
                        int deltaSec;
                        cmd.CommandText = "INSERT INTO Curtail_Readings(meterID,timestamp,value) VALUES(" + Convert.ToString(pk) + ",?,?)";

                        // first line is format data
                        line = sr.ReadLine();
                        deltaSec = Convert.ToInt32(line.Split(',')[3]);

                        IDbDataParameter param1 = cmd.CreateParameter();
                        IDbDataParameter param2 = cmd.CreateParameter();  

                        cmd.Parameters.Add(param1);
                        cmd.Parameters.Add(param2);

                        while((line = sr.ReadLine()) != null) 
                        {
                            string[] cols = line.Split(',');
                            DateTime timestamp = DecodeDate(cols[0]);

                            if((data_present && (timestamp > DecodeDate(datestamp))) || !data_present)
                            {
                                for(int i = 1;i < cols.Length;i++)
                                {
                                    param1.Value = Convert.ToString(timestamp);
                                    param2.Value = cols;
                                    SqliteCommand sqlite_cmd = (SqliteCommand)cmd;
                                    crash_str = sqlite_cmd.BindParameters2();
                                    crash_i = i;
                                    cmd.ExecuteNonQuery();
                                    timestamp = timestamp.AddSeconds(deltaSec);
                                }
                                MessageBox.Show("Loop");
                            }
                        }

                    }

                    MessageBox.Show("Now Finishing");

                    trans.Commit();
                }
            }
        }
        catch(Exception e)
        {
            MessageBox.Show("Unable to read file in loop " + Convert.ToString(crash_i) + ":\n" + file + "\n Error: '" + e.ToString() + "'\n" + crash_str + ".","Error!");
        }

        return true;
    }

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!