Index: src/Core/Banshee.Services/Banshee.SmartPlaylist/SmartPlaylistSource.cs
===================================================================
--- src/Core/Banshee.Services/Banshee.SmartPlaylist/SmartPlaylistSource.cs	(revision 5029)
+++ src/Core/Banshee.Services/Banshee.SmartPlaylist/SmartPlaylistSource.cs	(working copy)
@@ -294,7 +294,7 @@
                     VALUES (?, ?, ?, ?, ?, ?, ?)",
                 Name, ConditionXml,
                 IsLimited ? QueryOrder.Name : null,
-                IsLimited ? LimitValue.ToSql () : null,
+                IsLimited ? LimitValue.ToSql (null) : null,
                 IsLimited ? Limit.Name : null,
                 PrimarySourceId, IsTemporary
             ));
@@ -315,7 +315,7 @@
                     WHERE SmartPlaylistID = ?",
                 Name, ConditionXml,
                 IsLimited ? QueryOrder.Name : null,
-                IsLimited ? LimitValue.ToSql () : null,
+                IsLimited ? LimitValue.ToSql (null) : null,
                 IsLimited ? Limit.Name : null,
                 Count, IsTemporary, DbId
             ));
Index: src/Libraries/Hyena/Hyena.Query/IntegerQueryValue.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/IntegerQueryValue.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/IntegerQueryValue.cs	(working copy)
@@ -102,7 +102,7 @@
             get { return Int64.MaxValue; }
         }
 
-        public override string ToSql ()
+        public override string ToSql (Operator op)
         {
             return Convert.ToString (value, System.Globalization.CultureInfo.InvariantCulture);
         }
Index: src/Libraries/Hyena/Hyena.Query/QueryOperator.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/QueryOperator.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/QueryOperator.cs	(working copy)
@@ -67,7 +67,8 @@
             get { return is_not; }
         }
         
-        internal Operator (string name, string label, string sql_format, params string [] userOps) : this (name, label, sql_format, false, userOps)
+        internal Operator (string name, string label, string sql_format, params string [] userOps) 
+                   : this (name, label, sql_format, false, userOps)
         {
         }
 
Index: src/Libraries/Hyena/Hyena.Query/StringQueryValue.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/StringQueryValue.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/StringQueryValue.cs	(working copy)
@@ -38,13 +38,15 @@
 {
     public class StringQueryValue : QueryValue
     {
-        public static readonly Operator Contains       = new Operator ("contains", Catalog.GetString ("contains"), "LIKE '%{0}%'", ":");
-        public static readonly Operator DoesNotContain = new Operator ("doesNotContain", Catalog.GetString ("doesn't contain"), "NOT LIKE '%{0}%'", true, "!:");
+        public static readonly Operator Contains       = new Operator ("contains", Catalog.GetString ("contains"), "LIKE '%{0}%'" + ESCAPE_CLAUSE, ":");
+        public static readonly Operator DoesNotContain = new Operator ("doesNotContain", Catalog.GetString ("doesn't contain"), "NOT LIKE '%{0}%'" + ESCAPE_CLAUSE, true, "!:");
         public static readonly Operator Equal          = new Operator ("equals", Catalog.GetString ("is"), "= '{0}'", "==");
         public static readonly Operator NotEqual       = new Operator ("notEqual", Catalog.GetString ("is not"), "!= '{0}'", true, "!=");
-        public static readonly Operator StartsWith     = new Operator ("startsWith", Catalog.GetString ("starts with"), "LIKE '{0}%'", "=");
-        public static readonly Operator EndsWith       = new Operator ("endsWith", Catalog.GetString ("ends with"), "LIKE '%{0}'", ":=");
+        public static readonly Operator StartsWith     = new Operator ("startsWith", Catalog.GetString ("starts with"), "LIKE '{0}%'" + ESCAPE_CLAUSE, "=");
+        public static readonly Operator EndsWith       = new Operator ("endsWith", Catalog.GetString ("ends with"), "LIKE '%{0}'" + ESCAPE_CLAUSE, ":=");
 
+        private const string ESCAPE_CLAUSE = " ESCAPE '\\'";
+
         protected string value;
 
         public override string XmlElementName {
@@ -77,9 +79,29 @@
             ParseUserQuery (str);
         }
 
-        public override string ToSql ()
+        public override string ToSql (Operator op)
         {
-            return String.IsNullOrEmpty (value) ? null : value.Replace ("'", "''");
+            string ret = null;
+            if (String.IsNullOrEmpty (value))
+                return ret;
+
+            if (op == null)
+                throw new ArgumentNullException ("op");
+
+            ret = value.Replace ("'", "''");
+            if (op == Contains ||
+                op == DoesNotContain ||
+                op == StartsWith ||
+                op == EndsWith)
+                ret = EscapeLike (ret);
+            return ret;
         }
+
+        private static string EscapeLike (string orig)
+        {
+            return orig.Replace ("\\", "\\\\")
+                       .Replace ("%", "\\%")
+                       .Replace ("_", "\\_");
+        }
     }
 }
Index: src/Libraries/Hyena/Hyena.Query/NullQueryValue.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/NullQueryValue.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/NullQueryValue.cs	(working copy)
@@ -81,7 +81,7 @@
         {
         }
 
-        public override string ToSql ()
+        public override string ToSql (Operator op)
         {
             return null;
         }
Index: src/Libraries/Hyena/Hyena.Query/Tests/QueryTests.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/Tests/QueryTests.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/Tests/QueryTests.cs	(working copy)
@@ -81,22 +81,22 @@
             qv = new DateQueryValue (); qv.ParseUserQuery ("2007-03-9");
             Assert.AreEqual (new DateTime (2007, 3, 9), qv.Value);
             Assert.AreEqual ("2007-03-09", qv.ToUserQuery ());
-            Assert.AreEqual ("1173420000", qv.ToSql ());
+            Assert.AreEqual ("1173420000", qv.ToSql (null));
     
             qv = new StringQueryValue (); qv.ParseUserQuery ("foo 'bar'");
             Assert.AreEqual ("foo 'bar'", qv.Value);
             Assert.AreEqual ("foo 'bar'", qv.ToUserQuery ());
-            Assert.AreEqual ("foo ''bar''", qv.ToSql ());
+            Assert.AreEqual ("foo ''bar''", qv.ToSql (null));
     
             qv = new IntegerQueryValue (); qv.ParseUserQuery ("22");
             Assert.AreEqual (22, qv.Value);
             Assert.AreEqual ("22", qv.ToUserQuery ());
-            Assert.AreEqual ("22", qv.ToSql ());
+            Assert.AreEqual ("22", qv.ToSql (null));
     
             qv = new FileSizeQueryValue (); qv.ParseUserQuery ("2048 KB");
             Assert.AreEqual (2097152, qv.Value);
             Assert.AreEqual ("2.048 KB", qv.ToUserQuery ());
-            Assert.AreEqual ("2097152", qv.ToSql ());
+            Assert.AreEqual ("2097152", qv.ToSql (null));
     
             // TODO this will break once an it_IT translation for "days ago" etc is committed
             qv = new RelativeTimeSpanQueryValue (); qv.ParseUserQuery ("2 days ago");
@@ -107,7 +107,7 @@
             qv = new TimeSpanQueryValue (); qv.ParseUserQuery ("4 minutes");
             Assert.AreEqual (240, qv.Value);
             Assert.AreEqual ("4 minutes", qv.ToUserQuery ());
-            Assert.AreEqual ("240000", qv.ToSql ());
+            Assert.AreEqual ("240000", qv.ToSql (null));
         }
     
         [Test]
Index: src/Libraries/Hyena/Hyena.Query/RelativeTimeSpanQueryValue.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/RelativeTimeSpanQueryValue.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/RelativeTimeSpanQueryValue.cs	(working copy)
@@ -78,7 +78,7 @@
             node.SetAttribute ("type", "rel");
         }
 
-        public override string ToSql ()
+        public override string ToSql (Operator op)
         {
             return DateTimeUtil.FromDateTime (DateTime.Now + TimeSpan.FromSeconds ((double) offset)).ToString (System.Globalization.CultureInfo.InvariantCulture);
         }
Index: src/Libraries/Hyena/Hyena.Query/DateQueryValue.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/DateQueryValue.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/DateQueryValue.cs	(working copy)
@@ -106,7 +106,7 @@
             }
         }
 
-        public override string ToSql ()
+        public override string ToSql (Operator op)
         {
             return DateTimeUtil.FromDateTime (value).ToString (System.Globalization.CultureInfo.InvariantCulture);
         }
Index: src/Libraries/Hyena/Hyena.Query/QueryValue.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/QueryValue.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/QueryValue.cs	(working copy)
@@ -141,6 +141,6 @@
             return Value.ToString ();
         }
 
-        public abstract string ToSql ();
+        public abstract string ToSql (Operator op);
     }
 }
Index: src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs	(working copy)
@@ -158,7 +158,7 @@
             node.SetAttribute ("factor", factor.ToString ());
         }
 
-        public override string ToSql ()
+        public override string ToSql (Operator op)
         {
             return Convert.ToString (offset * 1000, System.Globalization.CultureInfo.InvariantCulture);
         }
Index: src/Libraries/Hyena/Hyena.Query/QueryLimit.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/QueryLimit.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/QueryLimit.cs	(working copy)
@@ -75,7 +75,7 @@
 
         public string ToSql (IntegerQueryValue limit_value)
         {
-            return RowBased ? String.Format ("LIMIT {0}", limit_value.ToSql ()) : null;
+            return RowBased ? String.Format ("LIMIT {0}", limit_value.ToSql (null)) : null;
         }
     }
 }
Index: src/Libraries/Hyena/Hyena.Query/QueryField.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Query/QueryField.cs	(revision 5029)
+++ src/Libraries/Hyena/Hyena.Query/QueryField.cs	(working copy)
@@ -143,7 +143,7 @@
 
         public string ToSql (Operator op, QueryValue qv)
         {
-            string value = qv.ToSql () ?? String.Empty;
+            string value = qv.ToSql (op) ?? String.Empty;
 
             if (op == null) op = qv.OperatorSet.First;
 
Index: src/Libraries/Hyena.Gui/Hyena.Query.Gui/QueryLimitBox.cs
===================================================================
--- src/Libraries/Hyena.Gui/Hyena.Query.Gui/QueryLimitBox.cs	(revision 5029)
+++ src/Libraries/Hyena.Gui/Hyena.Query.Gui/QueryLimitBox.cs	(working copy)
@@ -116,7 +116,7 @@
 
             set {
                 if (value != null && !value.IsEmpty)
-                    count_entry.Text = value.ToSql ();
+                    count_entry.Text = value.ToSql (null);
             }
         }
 
