我有一个SQL脚本,我必须在其中插入&符号(&),我不知道它所处的位置。我从无法使用SET DEFINE OFF的环境运行此SQL脚本。我只能运行SQL插入,选择,更新和删除。
答案 0 :(得分:0)
您可以使用import psycopg2
from config import config
from psycopg2 import sql
import os
tablename = 'Worksorders'
tempBaseURL = 'C:\Administrator Files'
RawCSVBaseURL = 'G:\Team Drives\RawCSV'
fileType = '.csv'
global fullTempBaseURL
global fullRawCSVURL
fullTempBaseURL = tempBaseURL + '\\' + tablename + fileType
fullRawCSVURL = RawCSVBaseURL + '\\' + tablename + fileType
print "fullTempBaseURL is: " + fullTempBaseURL
print "fullRawCSVURL is: " + fullRawCSVURL
###########################
#Copy table to CSV
###########################
def tabletoCSV():
conn = None
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the SQL statement
cur.execute("SET search_path TO dbo")
cur.execute(sql.SQL("""copy (SELECT * FROM {tbl} ) to {url} with csv""").format(tbl = sql.Identifier(tablename), url = sql.Identifier(fullTempBaseURL)))
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def moveFile():
os.rename(fullTempBaseURL, fullRawCSVURL)
tabletoCSV()
来逃避&
。
chr(38)
结果
select chr(38) as res from dual
您可以将此符号连接到任何位置的任何字符串:
res
&
结果
select 'M ' || chr(38) || ' S' as res from dual