[英]Executing a sql file in psycopg2
此功能或多或少是我們為技術支持人員提供的實用程序的一部分,旨在為他們提供一種從postgres備份中提取表,重命名表並將其重新插入另一個數據庫的簡便方法。 在使用cur.execute()將表實際插入所選數據庫之前,我的函數可以正常工作。 sed(是的,我想堅持使用sed),提取部分的效果很好。 我想用psycopg2以某種方式執行該文件,但是如果有人有使用子進程和psql的想法,我願意接受其他建議。 通過表名列表,目標數據庫和sql文件備份調用此函數。 我看過其他使用.read()的示例。 我也知道我應該將文件名轉換為變量以清理代碼。 如果可能,它還需要使用python 2.6。
for tbl in selected_tables:
# Open a file each iteration, dump the table in to it. Use .wait() to make sure the command finishes.
table_file = open(str(tbl) + backup_suffix + ".sql", "w")
p1 = subprocess.Popen(["/usr/bin/pg_restore", backup_picked, "-t", tbl], stdout=table_file)
p1.wait()
# Rename every instance of the table in the restore file. Format: _backup_YYYYMMDD
sed_guts = "s/" + str(tbl) + "/" + str(tbl) + backup_suffix + "/g"
p2 = subprocess.Popen(["/bin/sed", "-i", sed_guts, str(tbl) + backup_suffix + ".sql"])
p2.wait()
table_file.close()
# Restore the tables.
try:
# Gets the proper connection information. Works fine.
site_config = site.ParseConfig(target_site)
contents = open(str(tbl) + backup_suffix + ".sql", "r")
con = psycopg2.connect(user=site_config['dbusername'],host=site_config['host'], \
password= site_config['password'], port=site_config['port'],\
dbname=site_config['dbname'])
con.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
# Connect to postgres or to the db and then execute the query.
cur = con.cursor()
# Fails here
cur.execute(contents.read())
os.remove(str(tbl) + backup_suffix + ".sql")
except:
os.remove(str(tbl) + backup_suffix + ".sql")
最后,我最終還是回到了psql和subprocess.Popen上,shell = True。 這不是我的首選方法,因為我還有其他限制,所以我願意走這條路。 如果有人好奇,這就是我所做的。
backup_suffix = site.FileSuffix(backup_picked)
# Do the actual restoration here. Use sed to rename every instance in the files that will be created below.
# Make sure the created files get removed before finishing.
for tbl in selected_tables:
table_file_name = str(tbl) + backup_suffix + ".sql"
# Open a file each iteration, dump the table in to it. Use .wait() to make sure the command finishes.
table_file = open(table_file_name, "w")
p1 = subprocess.Popen(["/usr/bin/pg_restore", backup_picked, "-t", tbl], stdout=table_file)
p1.wait()
table_file.close()
# Rename every instance of the table in the restore file. Format: _backup_YYYYMMDD
sed_guts = "s/" + str(tbl) + "/" + str(tbl) + backup_suffix + "/g"
p2 = subprocess.Popen(["/bin/sed", "-i", sed_guts, table_file_name])
p2.wait()
# Use psql to restore the tables.
try:
site_config = site.ParseConfig(target_site)
dontuse, site_config = addFlags(site_config, site_config)
command = '/usr/bin/psql %s %s %s %s < %s' % (site_config['host'], site_config['dbusername'],
site_config['dbname'], site_config['port'], table_file_name)
p2 = subprocess.Popen('%s' % command, shell=True)
p2.communicate()[0]
os.remove(table_file_name)
except:
os.remove(table_file_name)
session.CleanUp()
exit(0)
還有多種方法可以在字符串中寫入多個變量。 如果有人好奇,這里有個幫助鏈接: 在Python中使用多個參數進行字符串格式化(例如'%s ...%s')
這是另一個用於在Python中打開和關閉文件的好資源: http : //www.tutorialspoint.com/python/python_files_io.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.