1

想知道是否有人有一个聪明的解决方案来修复 Python 中的错误插入语句,由一个不太聪明的程序导出。它没有为字符串中带有单引号的字符串添加两个单引号。为了使它更容易一点,所有插入的值都是字符串。

所以它有:

INSERT INTO addresses VALUES ('1','1','CUCKOO'S NEST','CUCKOO'S NEST STREET');

代替:

INSERT INTO addresses VALUES ('1','1','CUCKOO''S NEST','CUCKOO''S NEST STREET');

显然这有多行,我也不想替换封闭的单引号。

正在考虑使用拆分和连接,但我不确定如何在循环中轻松更新拆分值。对不起,我是菜鸟。类似下面的东西,我不知道该怎么做#update bit

import sys

fileIN = open('a.sql', "r")
line = fileIN.readline()

while line:
   bits = line.split("','")
   for bit in bits:
      if bit.find("'") > -1:
         #update bit
   line_out = "','".join(bits)      
   sys.stdout.write(line_out)
   line = fileIN.readline()

谢谢

4

5 回答 5

7

根据 katrielalex 的建议,这个怎么样:

>>> import re
>>> s = "INSERT INTO addresses VALUES ('1','1','CUCKOO'S NEST','CUCKOO'S NEST STREET');"
>>> def repl(m):
    if m.group(1) in ('(', ',') or m.group(2) in (',', ')'):
        return m.group(0)
    return m.group(1) + "''" + m.group(2)

>>> re.sub("(.)'(.)", repl, s)
"INSERT INTO addresses VALUES ('1','1','CUCKOO''S NEST','CUCKOO''S NEST STREET');"

如果你对消极的后向断言,这是令人头痛的纯正则表达式版本:

re.sub("((?<![(,])'(?![,)]))", "''", s)
于 2012-07-23T12:19:37.997 回答
0

一起去:

import sys
import re

def repl(m):
    if m.group(1) in ('(', ',') or m.group(2) in (',', ')'):
        return m.group(0)
    return m.group(1) + "''" + m.group(2)

fileIN = open('a.sql', "r")
line = fileIN.readline()

while line:
    line_out = re.sub("(.)'(.)", repl, line)
    sys.stdout.write(line_out)
    # Next line.
    line = fileIN.readline()
于 2012-07-24T10:01:44.743 回答
0
while line:
    # Restrain line2 to inside parentheses
    line1, rest = line.split('(')
    line2, line3 = rest.split(')')
    # A bit more cleaner
    new_bits = []
    for bit in line2.split(','):
        # Remove border ' characters
        bit = bit[1:-1]
        # Duplicate the ones inside
        if "'" in bit:
            bit = bit.replace("'", "''")
        # Re-add border '
        new_bits.append("'" + bit + "'")
   sys.stdout.write(line1 + '(' + ','.join(new_bits + ')' + line3)
   line = fileIN.readline()
于 2012-07-23T12:11:02.930 回答
0

警告:这在很大程度上取决于SQL 语句格式。但是,如果您的输入只会采用“语句(参数)结束”格式,那么这将每次都有效。

import sys

fileIN = open('a.sql', "r")
line = fileIN.readline()

while line:
    #split out the parameters (between the ()'s)
    start, temp = line.split("(")
    params, end = temp.split(")")

    #replace the "'"s in the parameters (without the start and end quote)
    newParams = "','".join([x.replace("'", "''") for x in params[1:-1].split("','")])

    #join the statement back together
    line_out = start + "('" + newParams + "')" + end

    #next line
    sys.stdout.write(line_out)
    line = fileIN.readline()

解释:

  1. 将字符串拆分为 3 部分:查询开始、参数和结束。
  2. 生成器获取参数(没有开始/结束's),将其拆分为',',并且对于拆分生成的列表中的每个元素(单个数据条目),将's替换为''s。
  3. 最后一行然后连接查询开始、新参数(带有先前删除的括号和引号)和语句的结尾。
于 2012-07-23T12:51:09.527 回答
0

另一个答案:

a = "INSERT INTO addresses VALUES ('1','1','CUCKOO'S NEST','CUCKOO'S NEST STREET');"
open_par = a.find("(")
close_par = a.find(")")
b = a[open_par+1:close_par]
c = b.split(",")
d = map(lambda x: '"' + x.strip().strip("'") + '"',c)
result = a[:open_par+1] + ",".join(d) + a[close_par:]
于 2012-07-23T14:05:03.107 回答