sql工作正常,但使用python它不会将值插入表中

时间:2017-08-02 14:58:48

标签: postgresql python-3.x commit sql-insert psycopg2

我正在尝试在sql中使用python存储过程,我已经测试了我的sql代码并且工作正常,但是当我通过python执行它时,值不会插入到我的表中 注意:执行

时没有任何错误

我的代码如下:

import psycopg2
con = psycopg2.connect(dbname='dbname'
                    , host='host'
                    , port='5439', user='username', password='password')

def executeScriptsfromFile(filename):
    #Open and read the file as a single buffer
    cur = con.cursor()
    fd = open(filename,'r')
    sqlFile = fd.read()
    fd.close()

    #all SQL commands(split on ';')
    sqlCommands = filter(None,sqlFile.split(';'))

    #Execute every command from the input file
    for command in sqlCommands:
        # This will skip and report errors
        # For example, if the tables do not yet exist, this will skip over
        # the DROP TABLE commands
         try:
            cur.execute(command)
            con.commit()
         except Exception as inst:
            print("Command skipped:", inst)

    cur.close()
executeScriptsfromFile('filepath.sql')

在sql中插入注释:

INSERT INTO schema.users
SELECT
    UserId 
    ,Country 
    ,InstallDate
    ,LastConnectDate
    FROM #Source;

注意:正如我所说的,当我测试它时,sql的工作完全正常。

0 个答案:

没有答案