in

System.Data.SQLite

An open-source, enhanced version of the SQLite database engine for Windows

Exception when using alternate parameter names.

Last post 02-27-2008 9:58 AM by Nate. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 02-25-2008 11:51 PM

    • tarix
    • Top 500 Contributor
    • Joined on 02-26-2008
    • Posts 2

    Exception when using alternate parameter names.

    The following code works flawlessly:

            public int put_item(Item xitem)

            {
                using (SQLiteTransaction transaction = my_connection.BeginTransaction())
                {
                    using (SQLiteCommand command = new SQLiteCommand(my_connection))
                    {
                        SQLiteParameter item_id = command.CreateParameter();
                        item_id.Value = xitem.my_id;

                        SQLiteParameter item_name = command.CreateParameter();
                        item_name.Value = xitem.my_name;

                        SQLiteParameter item_icon = command.CreateParameter();
                        item_icon.Value = xitem.my_icon;

                        SQLiteParameter item_level = command.CreateParameter();
                        item_level.Value = xitem.my_level;

                        SQLiteParameter item_quality = command.CreateParameter();
                        item_quality.Value = xitem.my_quality;

                        SQLiteParameter item_type = command.CreateParameter();
                        item_type.Value = xitem.my_type;
                       
                        command.CommandText = "insert into items (id, name, icon, level, quality, type) values (?1, ?2, ?3, ?4, ?5, ?6);";
                        command.Parameters.Add(item_id);
                        command.Parameters.Add(item_name);
                        command.Parameters.Add(item_icon);
                        command.Parameters.Add(item_level);
                        command.Parameters.Add(item_quality);
                        command.Parameters.Add(item_type);

                        command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }

                return 0;
            }

     If I change the query to either of these two:

                        command.CommandText = "insert into items (id, name, icon, level, quality, type) values (?1, ?2, ?3, ?4, ?5, ?6);";
                        command.CommandText = "insert into items (id, name, icon, level, quality, type) values (:a, :b, :c, :d, :e, :f);";

     Then I get an exception from the ExecuteNonQuery() function call:

     An unhandled exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll

    Additional information: SQLite error
    Insufficient parameters supplied to the command

    (Interestingly enough I had the opposite problem when I was using the C library directly in another project.  I couldn't get queries with only ?'s to work.)
     

  • 02-27-2008 9:58 AM In reply to

    • Nate
    • Top 10 Contributor
    • Joined on 08-28-2005
    • Fort Collins, CO
    • Posts 65

    Re: Exception when using alternate parameter names.

    This is how an ADO .NET Data Provider should behave. However, the one you say works flawlessly should not work at all (you meant ?, ?, ?... right?).

    The three common and supported patterns are:

    1.) A set of ? with no name (works by going in order from left to right)

     using (SQLiteCommand command = connection.CreateCommand())
    {
        command.CommandText = "INSERT INTO test VALUES (?, ?);";
        command.Parameters.AddWithValue("value1", "val1");
        command.Parameters.AddWithValue("value2", 1);
        command.ExecuteNonQuery();
    }

     

    2.) An at signed prefixed name 

    using (SQLiteCommand command = connection.CreateCommand())
    {
        command.CommandText = "INSERT INTO test VALUES (@value1, @value2);";
        command.Parameters.AddWithValue("value1", "val2");
        command.Parameters.AddWithValue("value2", 2);
        command.ExecuteNonQuery();
    }

    3.) A colon prefixed name 

    using (SQLiteCommand command = connection.CreateCommand())
    {
        command.CommandText = "INSERT INTO test VALUES (:value1, :value2);";
        command.Parameters.AddWithValue("value1", "val3");
        command.Parameters.AddWithValue("value2", 3);
        command.ExecuteNonQuery();
    }

     

    Why do you get the impression that ?1, ?2 should work? Does another provider do that? And :a, :b, :c, :d is fine, you just did not include those names in the paramaters you added. 

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems