#!/usr/bin/env python """ Escape a string for feeding to MySQL. This script works without a connection, by escaping any character that is not valid SQL in iso-8859-1 encoding. """ # $URL: https://opensvn.csie.org/TinyCollection/database/trunk/script/mysqlescape.py $ __author__ = '$Author: DonQuichote $'[8:-2] __version__ = '$Rev: 756 $'[5:-2] __date__ = '$Date: 2009-10-06 15:07:56 +0200 (Tue, 06 Oct 2009) $'[6:-2] import os.path import string import sys def EscapeString(text, asHex=False): result = "" escapes = {"\n":"\\n", "\\":"\\\\", "'":"''", "\t":"\\t", "\r":"\\r"} position = 0 if text=="": asHex = False while (positionord("~")): asHex = True else: result = result + chr position = position + 1 if asHex: result = "0x" position = 0 while position