【问题标题】:Error when move data from database to another将数据从数据库移动到另一个时出错
【发布时间】:2019-10-01 00:09:11
【问题描述】:

python 上的 ETL 代码错误

我设法在 python 上学习了一些代码行,以便在 MS SQL 环境中执行 ETL 过程。初始脚本用于 PostgreSQL 环境。我想将我的用于 MS SQL。我尝试编辑代码,但出现错误。请大家看看

import petl as etl, pyodbc as py, sys
from sqlalchemy import *

reload(sys)
sys.setdefaultencoding('utf8')

dbCnxns = {'sample' :"dbname=sample user=user host=127.0.0.1" 
           , 'python': "dbname=python user=user host=127.0.0.1" }

#set my connection
sourceConn = py.connect(dbCnxns['sample'])
targetConn = py.connect(dbCnxns['python'])
sourceCursor = sourceConn.cursor()
targetCursor = targetConn.cursor()

sourceCursor.execute = ('SELECT name from sys.tables')


sourceTables = sourceCursor.fetchall()

for t in sourceTables:
    targetCursor.execute("drop table if exist %s" % (t[0]))
    sourceDs = etl.fromdb(sourceConn, "select * from %s" % (t[0]))
    etl.todb(sourceDs,targetConn,t[0], create=True, sample=1000)

谢谢

经过一些修改。我能够为 MSSQL D 编写代码。这是之前的代码

import petl as etl, pyodbc as py
#from sqlalchemy import *

#reload(sys)
#sys.setdefaultencoding('utf8')

#dbCnxns = {'sample' : "Driver={SQL Server} Server=USER-PC Database=sample Trusted_Connection=yes" 
#           , 'python': "Driver={SQL Server} Server=USER-PC Database=python Trusted_Connection=yes" }

#set my connection
#sourceConn = pg.connect(dbCnxns['sample'])
#targetConn = pg.connect(dbCnxns['python'])
#sourceCursor = sourceConn.cursor()
#targetCursor = targetConn.cursor()

#sourceCursor.execute = (***SELECT * FROM sample.dbo.Customer***)


sourceConn = py.connect('Driver={SQL Server};'
                      'Server=USER-PC;'
                      'Database=sample;'
                      'Trusted_Connection=yes;')

targetConn = py.connect('Driver={SQL Server};'
                      'Server=USER-PC;'
                      'Database=python;'
                      'Trusted_Connection=yes;')

sourceCursor = sourceConn.cursor()
targetCursor = targetConn.cursor()

sourceCursor.execute('SELECT name from sys.tables')

sourceTables = sourceCursor.fetchall()

for t in sourceTables:
    targetCursor.execute("drop table if exist %s" % (t[0]))
    sourceDs = etl.fromdb(sourceConn, "select * from %s" % (t[0]))
    etl.todb(sourceDs,targetConn,t[0], create=True, sample=1000)

现在,我看起来不错,但是遇到了编程错误

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]关键字'if'附近的语法不正确。(156) (SQLExecDirectW); [42000] [Microsoft][ ODBC SQL Server 驱动程序][SQL Server]在“客户”附近的预期条件的上下文中指定的非布尔类型表达式。(4145)

Visit https://www.dofactory.com/sql/sample-database

查看我正在处理的数据库结构。

再次感谢您

【问题讨论】:

  • 由于我们没有您的数据库,因此无法轻松重现该问题-至少,您能否提供正在生成的错误的踪迹?
  • 错误日志运行文件('C:/Users/user/.spyder-py3/etl.py', wdir='C:/Users/user/.spyder-py3') Traceback (最新最后调用):文件“C:\Users\user\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”,第 3296 行,在 run_code exec(code_obj, self.user_global_ns, self.user_ns) 文件“",第 1 行,在 runfile('C:/Users/user/.spyder-py3/etl.py', wdir='C:/Users/user/.spyder- py3') 文件“C:/Users/user/.spyder-py3/etl.py”,第 23 行 sourceCursor.execute = (**select name ^ SyntaxError: invalid syntax
  • 将第 23 行替换为 sourceCursor.execute('SELECT name from sys.tables') 语法
  • targetCursor.execute("SELECT @@VERSION").fetchval() 返回什么?
  • 删除表,如果它存在于目标数据库中......任何想法@Grismar

标签: python sqlalchemy etl pyodbc petl


【解决方案1】:

Microsoft SQL Server 2016 中引入了对DROP TABLE IF EXISTS ... 的支持。您显然使用的是早期版本的 SQL Server,因此您必须使用解决方法。详情请见this question

【讨论】:

  • 你是对的。我正在使用 SQL Server 2014。我将更新我的服务器并重新运行脚本。谢谢
猜你喜欢
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 2011-05-19
  • 2023-01-11
  • 2020-07-02
相关资源
最近更新 更多