SQLite error on 4th write to table

My code is:

        
public void WriteTemperatures(string streha, string dno)
{
    Debug.Print("INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (" + streha + "," + dno + ");");
    _DB.ExecuteNonQuery("INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (" + streha + "," + dno + ");");
}

I call WriteTemperatures every 2 seconds.
Output window show:

[quote]
INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (5,39);
INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (13,77);
INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (41,0);
INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (38,26);
#### Exception GHI.Premium.SQLite.SQLiteException - 0x00000000 (1) ####
#### Message: Could not execute SQL statement.
#### GHI.Premium.SQLite.Database::ExecuteNonQuery [IP: 0022] ####
#### SmartHouse.Program::TemperaturesAdd [IP: 0026] ####
#### SmartHouse.MatIcon::MatIcon_TouchUp [IP: 0008] ####
#### Microsoft.SPOT.Input.TouchEventHandler::Invoke [IP: 80165552] ####
#### Microsoft.SPOT.Presentation.UIElement::OnTouchUp [IP: 000f] ####
#### Microsoft.SPOT.RoutedEventArgs::InvokeHandler [IP: 0029] ####
#### Microsoft.SPOT.EventRoute::InvokeHandlers [IP: 00a3] ####
#### Microsoft.SPOT.Presentation.UIElement::RaiseEvent [IP: 0052] ####
#### Microsoft.SPOT.Input.InputManager::ProcessStagingArea [IP: 00a0] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
A first chance exception of type ‘GHI.Premium.SQLite.SQLiteException’ occurred in GHI.Premium.SQLite.dll
An exception of type ‘GHI.Premium.SQLite.SQLiteException’ occurred in GHI.Premium.SQLite.dll but was not handled in user code

Additional information: Could not execute SQL statement.[/quote]

I try to run the code 10 times and it always get an error on 4th call.

@ Makla, when asking support questions please provide us with the mainboard and version of the SDK you are using. This makes it easier to provide support as well as it allows us to use your exact setup when testing your code.

My mainboard is Spider. I don’t know where to find SDK. My Release Build Info.txt is:

This was the setup, that i run. If it matters, i create database and tables with SQLite studio.
Does this help?

@ Makla - You can determine the version of the SDK by going into MFDeploy, and select USB, then your board, click the menu labeled Plugin -> Debug -> Show Device Info.

Hi. Need some time. I never used MFDeploy before.

We could not reproduce the error using the latest SDK released yesterday (4.2.7) using this code:


        static Database myDatabase;
        static string dbRoot;

        static PersistentStorage storage;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
        }

        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Removable Media Ejected");
        }

        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Removable Media Inserted");

            //SQLite
            try
            {

                // Create new database file
                myDatabase = new Database();
                dbRoot = e.Volume.RootDirectory;
                // Open a new Database in NAND Flash
                myDatabase.Open(dbRoot + "\\myDatabase.dbs");

                //add a table
                myDatabase.ExecuteNonQuery(
                  "CREATE Table Temperature" +
                  "(ID INTEGER, Room TEXT, Time INTEGER, Value DOUBLE)");

               
                for(int i = 0; i < 20; i++)
                    WriteTemps(0);

                ReadTemps();
            }
            catch (Exception error)
            {
                Debug.Print(error.Message);
                Debug.Print(Database.GetLastError());
            }
        }

        static void WriteTemps(int id)
        {
            myDatabase.ExecuteNonQuery(
             "INSERT INTO Temperature (ID, Room, Time,Value) " +
             "VALUES (" + id.ToString() + ",'living room',053000,9300)");
        }

        static void ReadTemps()
        {
            // Process SQL query and save returned records in SQLiteDataTable
            SQLiteDataTable table = myDatabase.ExecuteQuery("SELECT * FROM Temperature");

            // Get a copy of columns orign names example
            String[] origin_names = table.ColumnOriginNames;

            // Get a copy of table data example
            ArrayList[] tabledata = table.ColumnData;


            String temp = "Fields: ";
            for (int i = 0; i < table.Columns; i++)
            {
                temp += table.ColumnOriginNames[i] + " |";
            }
            Debug.Print(temp);
            object obj;
            for (int j = 0; j < table.Rows; j++)
            {
                temp = j.ToString() + " ";
                for (int i = 0; i < table.Columns; i++)
                {
                    obj = table.ReadRecord(i, j);
                    if (obj == null)
                        temp += "N/A";
                    else
                        temp += obj.ToString();
                    temp += " |";
                }
                Debug.Print(temp);

            }
        }

On first run, I passed in the value of i to WriteTemps() and then I statically passed 0 to WriteTemps, and this is the result respectively.

Removable Media Inserted
Fields: ID |Room |Time |Value |
0 0 |living room |53000 |9300 |
1 1 |living room |53000 |9300 |
2 2 |living room |53000 |9300 |
3 3 |living room |53000 |9300 |
4 4 |living room |53000 |9300 |
5 5 |living room |53000 |9300 |
6 6 |living room |53000 |9300 |
7 7 |living room |53000 |9300 |
8 8 |living room |53000 |9300 |
9 9 |living room |53000 |9300 |
10 10 |living room |53000 |9300 |
11 11 |living room |53000 |9300 |
12 12 |living room |53000 |9300 |
13 13 |living room |53000 |9300 |
14 14 |living room |53000 |9300 |
15 15 |living room |53000 |9300 |
16 16 |living room |53000 |9300 |
17 17 |living room |53000 |9300 |
18 18 |living room |53000 |9300 |
19 19 |living room |53000 |9300 |

//////////////////////////////////////////////////////////////////

Removable Media Inserted
Fields: ID |Room |Time |Value |
0 0 |living room |53000 |9300 |
1 0 |living room |53000 |9300 |
2 0 |living room |53000 |9300 |
3 0 |living room |53000 |9300 |
4 0 |living room |53000 |9300 |
5 0 |living room |53000 |9300 |
6 0 |living room |53000 |9300 |
7 0 |living room |53000 |9300 |
8 0 |living room |53000 |9300 |
9 0 |living room |53000 |9300 |
10 0 |living room |53000 |9300 |
11 0 |living room |53000 |9300 |
12 0 |living room |53000 |9300 |
13 0 |living room |53000 |9300 |
14 0 |living room |53000 |9300 |
15 0 |living room |53000 |9300 |
16 0 |living room |53000 |9300 |
17 0 |living room |53000 |9300 |
18 0 |living room |53000 |9300 |
19 0 |living room |53000 |9300 |

@ James

Malka is not creating the database on the device He says he is using SQLite Studio, which I assume is another program. There might be a compatibility issue?

The problem was, that i was opening database and then write SQL, then opening the same database and read it. I repeat this 2 steps with every call (insert something).
I should call Database.Close().


        public void WriteTemperatures(string streha, string dno)
        {
            _DB = new Database();  //VERY BAD IDEA
            _DB.Open(_SD.RootDirectory + @ "\DB\data.db");
            Debug.Print("INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (" + streha + "," + dno + ");");
            _DB.ExecuteNonQuery("INSERT INTO Temperatures (TempStreha, TempDnoBojlerja) VALUES (" + streha + "," + dno + ");");
            ReadTemperaturesAndWriteToSreen();
        }

I don’t know why always on fourth call (that means that the same database was opened 6 times) and when try to write to 7th opened database, the code broke with an error, who doesn’t tell much.

Apparently it has nothing to do, where database was created. That sound logic to me.