0

I am using sqlite for my .NET Compact Framework 3.5 Application on windows mobile v6.I am facing slowness issue if I insert large data .

Consider this situation: I have around 150 items in my ArrayList (say List1). And I have a database table with two columns (say itemid and isFound).

I have to iterate each element in List1 and check in my database whether the itemid is available or not

  • If itemid is available in the table update the isFound status to 1 .
  • If the itemid is not available insert and update the isFound status.

For this it takes around 30 seconds what is the fastest way to do it ?

Here is my current code:

public ArrayList InsertNewlyScannedItems(ref ArrayList newlyScannedItemList)
{
    ArrayList newInsertedItemList = new ArrayList();
    SQLiteConnection conn = new SQLiteConnection("Data Source=" + db + ";Version=3;");
    SQLiteDataReader rs = null;

    try
    {
        conn.Open();
        SQLiteCommand availableTable = conn.CreateCommand();

        IEnumerator en = newlyScannedItemList.GetEnumerator();

        while (en.MoveNext())
        {
            ItemInfo itmInfo = (ItemInfo)en.Current;                  
            string cmdText = "";
            cmdText = "Select id, isFound item where id = '" + itmInfo.id + "'";
            availableTable.CommandText = cmdText;
            rs = availableTable.ExecuteReader();

            if (rs.Read())
            {
                if (!itmInfo.id.Equals(""))
                {
                    availableTable.Dispose();
                    availableTable.CommandText = "UPDATE item SET isFound = @isFound Where id = @id";
                    availableTable.Parameters.AddWithValue("@isFound", itmInfo.isFound);
                    availableTable.Parameters.AddWithValue("@id", itmInfo.id);
                    availableTable.ExecuteNonQuery();                           
                }
            }
            else
            {
                availableTable.Dispose();
                cmdText = "INSERT INTO item(id, isFound)";
                cmdText += "VALUES ( '" + itmInfo.id  + "','"  + itmInfo.isFound + ")";
                availableTable.CommandText = cmdText;
                availableTable.ExecuteNonQuery();
            }
            newInsertedItemList.Add(itmInfo);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        if (rs != null)
        {
            //rs.Close();
            //rs.Dispose();
        }
        if (conn != null && conn.State == ConnectionState.Open)
        {
            conn.Close();
            conn = null;
        }
    }
    return newInsertedItemList;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3383301
  • 1,891
  • 3
  • 21
  • 49
  • 2
    I don't think this is a question fundamentally about SqlLite insert speed and shouldn't have been closed. The problem the programmer faces is generic - he is accessing his database in a loop. Use of IN clause for the first select would eliminate 30% of his database calls, and subsequent batching of the two UPDATE/INSERT operations would reduce this by eliminating the remaining 99%. A correct answer would show this op reducing to only three SQL calls. The answers given in the referenced Question do not address the structural poor performance in the code. I would ask to have this reopened. – PhillipH Aug 31 '16 at 08:51
  • @PhillipH : Can you help me , how to reduce the sql call for this code ?? – user3383301 Aug 31 '16 at 09:05

0 Answers0