[PATCH] SQLite escape single quote

Pedro Aguilar <[email protected]> Tue, 20 May 2014 12:25:00 +0200 (CEST)
Newsgroups gmane.linux.drivers.gnokii
Message-ID <[email protected]>
Hi,

This patch adds a function that escapes only single quotes by putting two single
quotes in a row as SQLite likes them, otherwise it gives an insert error.

Reference:
http://www.sqlite.org/lang_expr.html
in the section Literal Values

Regards,

Pedro Aguilar
http://paguilar.org

_______________________________________________
gnokii-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/gnokii-users
sqlite_escape_single_quote.patch (text/x-patch, 1.3 KB)
--- a/smsd/sqlite.c	2014-02-06 16:38:22.642678584 +0100
+++ b/smsd/sqlite.c	2014-05-20 12:10:33.432270227 +0200
@@ -75,7 +82,7 @@
     }
 
     text = g_string_sized_new(480);
-    g_string_append(text, strEscape((gchar *) data->user_data[0].u.text));
+    g_string_append(text, strEscapeSingleQuote((gchar *) data->user_data[0].u.text));
 
     time(&rawtime);
     timeinfo = localtime(&rawtime);
--- a/smsd/utils.c	2013-03-26 12:50:20.523568205 +0100
+++ b/smsd/utils.c	2014-05-20 12:11:58.900272961 +0200
@@ -38,3 +38,26 @@
   return (ret);
 }
 
+/* Escapes ' with '' */
+/* SQLite escapes the single quote by puttin two of them in a row, */
+/* it doesn't need to escape the backslash */
+/* Returned value needs to be free with g_free(). */
+gchar *strEscapeSingleQuote (const gchar *const s)
+{
+  GString *str = g_string_new (s);
+  register gint i = 0;
+  gchar *ret;
+  
+  while (str->str[i] != '\0')
+  {
+    if (str->str[i] == '\'')
+      g_string_insert_c (str, i++, '\'');
+    i++;
+  }
+  
+  ret = str->str;
+  g_string_free (str, FALSE);
+  
+  return (ret);
+}
+
--- a/smsd/utils.h	2013-03-26 12:50:20.523568205 +0100
+++ b/smsd/utils.h	2014-05-20 12:10:40.712270460 +0200
@@ -19,5 +19,6 @@
 #include <glib.h>
 
 extern gchar *strEscape (const gchar *const s);
+extern gchar *strEscapeSingleQuote (const gchar *const s);
 
 #endif /* __smsd_utils_h_ */