Ngugi1 / sqlite-net

Automatically exported from code.google.com/p/sqlite-net
0 stars 0 forks source link

Update() methods throws an exception #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
SQLite.cs Update() function throws exception due to not sufficient objects 
are provided to String.Format() method. 

To avoid this, Update() method is modified as below:

public int Update (string name, object obj)
        {
            var type = obj.GetType ();
            var props = Orm.GetColumns (type);
            var pk = Orm.GetPK (type);
            if (pk == null) {
                throw new NotSupportedException ("Cannot 
update " + name + ": it has no PK");
            }
            var cols = from p in props
                where p != pk
                select p;
            var vals = from c in cols
                select c.GetValue (obj, null);
            var ps = new List<object> (vals);            
            /*
            ps.Insert (0, pk.GetValue (obj, null));
            var q = string.Format ("update '{0}'({1}) values ({2}) where 
'{3}'=?", 
                type.Name, 
                string.Join (",", (from c in cols select "?").ToArray ()), 
                pk.Name);*/
            ps.Add(pk.GetValue(obj, null)); 
            var q = string.Format("UPDATE {0} SET {1} WHERE {2} = ? ",
                            type.Name,
                            string.Join(",", (from c in cols select c.Name 
+ " = ? ").ToArray()),
                            pk.Name);            
            return Execute(q, ps.ToArray());
        }

Original issue reported on code.google.com by rupp...@gmail.com on 3 Nov 2009 at 5:01

GoogleCodeExporter commented 9 years ago
Thank you very much for the fix. I am integrating your changes now.

Original comment by frank.al...@gmail.com on 23 Jan 2010 at 6:49

GoogleCodeExporter commented 9 years ago
Changes integrated in Rev 22.

Original comment by frank.al...@gmail.com on 23 Jan 2010 at 6:57