Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INSERT INTO sample VALUES('1','Am G'haubach');
- INSERT INTO sample VALUES('1','Am G''haubach');
- /* Re-write invalid characters to their SQL-safe alternatives */
- t = targetbuf;
- for(s = buf; s <= lastchar; s++) {
- switch(*s) {
- case ''':
- /* *t++ = '\'; */
- *t++ = '''';
- break;
- case '\':
- *t++ = '\';
- *t++ = '\';
- break;
- case 'n':
- *t++ = '\';
- *t++ = 'n';
- break;
- case 'r':
- *t++ = '\';
- *t++ = 'r';
- break;
- case 't':
- *t++ = '\';
- *t++ = 't';
- break;
- default:
- *t++ = *s;
- }
- }
- int n_quotes = 0;
- // Point to base of target buf ...
- t = targetbuf;
- while (*s) {
- switch (*s) {
- case ''':
- // Examine the next character in an attempt
- // to determine if this is a 'string context'
- if (*(s + 1) != ',' && *(s + 1) != ')' && n_quotes % 2 == 1) {
- // We are more than likely inside a string ...
- *(t++) = ''';
- *(t++) = ''';
- } else {
- *(t++) = *s;
- n_quotes++;
- }
- break;
- default:
- *(t++) = *s;
- break;
- }
- s++;
- }
- *t = '