import psycopg2
import sys
con = None
try:
con = psycopg2.connect(database='local', user='local', password='local',port='1970')
cur = con.cursor()
cur.execute('SELECT x FROM t')
f = open('test.sql', 'w')
for row in cur:
f.write("insert into t values (" + str(row) + ");")
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if con:
con.close()
import psycopg
conn = psycopg2.connect("dbname=test user=postgres") # change this according to your RDBMS configuration
cursor = conn.cursor()
table_name='YOUR_TABLE_HERE' # place your table name here
with open("table_dump.sql") as f:
cursor.execute("SELECT * FROM %s" % (table_name)) # change the query according to your needs
column_names = []
columns_descr = cursor.description
for c in columns_descr:
column_names.append(c[0])
insert_prefix = 'INSERT INTO %s (%s) VALUES ' % (table_name, ', '.join(column_names))
rows = cursor.fetchall()
for row in rows:
row_data = []
for rd in row:
if rd is None:
row_data.append('NULL')
elif isinstance(rd, datetime.datetime):
row_data.append("'%s'" % (rd.strftime('%Y-%m-%d %H:%M:%S') ))
else:
row_data.append(repr(rd))
f.write('%s (%s);\n' % (insert_prefix, ', '.join(row_data))) # this is the text that will be put in the SQL file. You can change it if you wish.
如果你的操作系统是Linux,你可以使用下面的代码。 首先,应该运行
apt-get install postgresql
。使用psycopg2建立数据连接。文档中有很多示例:
http://initd.org/psycopg/
配置好数据源后,通过将结果集打印到文件中,迭代“SELECT”语句的结果,生成“INSERT INTO”语句。基本上是一些相反的逻辑。
这样,如果时间到了,需要使用备份文件,只需运行SQL文件,将数据插入。。。
示例:
然后恢复:
干杯
我想到的第一个想法是转储调用pg_dump命令的表,类似于here提出的方法(但是google有很多替代方法)。
但是,由于备份策略要求您选择精确的日期,而不仅仅是表,因此您可能需要依赖一系列查询,然后我的建议是使用类似Psycopg的库。
编辑:
我不能提供一个完整的例子,因为我不知道:
下面的示例生成一个文件,该文件存储单个查询的结果。
相关问题 更多 >
编程相关推荐