Guest User

Untitled

a guest
Jun 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. INSERT INTO sample VALUES('1','Am G'haubach');
  2.  
  3. INSERT INTO sample VALUES('1','Am G''haubach');
  4.  
  5. /* Re-write invalid characters to their SQL-safe alternatives */
  6. t = targetbuf;
  7. for(s = buf; s <= lastchar; s++) {
  8. switch(*s) {
  9. case ''':
  10. /* *t++ = '\'; */
  11. *t++ = '''';
  12. break;
  13. case '\':
  14. *t++ = '\';
  15. *t++ = '\';
  16. break;
  17. case 'n':
  18. *t++ = '\';
  19. *t++ = 'n';
  20. break;
  21. case 'r':
  22. *t++ = '\';
  23. *t++ = 'r';
  24. break;
  25. case 't':
  26. *t++ = '\';
  27. *t++ = 't';
  28. break;
  29. default:
  30. *t++ = *s;
  31. }
  32. }
  33.  
  34. int n_quotes = 0;
  35. // Point to base of target buf ...
  36. t = targetbuf;
  37. while (*s) {
  38. switch (*s) {
  39. case ''':
  40. // Examine the next character in an attempt
  41. // to determine if this is a 'string context'
  42. if (*(s + 1) != ',' && *(s + 1) != ')' && n_quotes % 2 == 1) {
  43. // We are more than likely inside a string ...
  44. *(t++) = ''';
  45. *(t++) = ''';
  46. } else {
  47. *(t++) = *s;
  48. n_quotes++;
  49. }
  50. break;
  51. default:
  52. *(t++) = *s;
  53. break;
  54. }
  55. s++;
  56. }
  57. *t = '';
  58. puts(targetbuf);
  59.  
  60. INSERT INTO sample VALUES('1', 'Am G''haubach');
Add Comment
Please, Sign In to add comment