没有使用python将行插入mysql
我想从PostgreSQL向mySQL数据库插入一些记录(两者都是远程服务器),所以我使用下面的脚本,但情况是数据没有被插入mySQL数据库 有一个最终计数语句总是结果为零。请问原因是什么 还需要对该代码进行任何改进 请给我一些建议没有使用python将行插入mysql,python,mysql,postgresql,Python,Mysql,Postgresql,我想从PostgreSQL向mySQL数据库插入一些记录(两者都是远程服务器),所以我使用下面的脚本,但情况是数据没有被插入mySQL数据库 有一个最终计数语句总是结果为零。请问原因是什么 还需要对该代码进行任何改进 请给我一些建议 import psycopg2 import os import time #import MySQLdb from mysql.connector import (connection) import sys #from pprint import pprint
import psycopg2
import os
import time
#import MySQLdb
from mysql.connector import (connection)
import sys
#from pprint import pprint
#from datetime import datetime
#from psycopg2 import sql
#from utils.config import Configuration as Config
#from utils.postgres_helper import get_connection
#from utils.utils import get_global_config
def db_connect():
# MySQLdb connection
try:
source_host = 'magento'
#conf = get_global_config()
#cnx_msql = MySQLdb.connect(host=conf.get(source_host, 'host'),
#user=conf.get(source_host, 'user'),
#passwd=conf.get(source_host, 'password'),
#port=int(conf.get(source_host, 'port')),
#db=conf.get(source_host, 'db'))
cnx_msql = connection.MySQLConnection(host='server.com', user='db13009',passwd='fgDT***********',port=3306,db='db13009')
print('MySQL DB connected')
except mysql.connector.Error as e:
print ("MYSQL: Unable to connect!", e.msg)
sys.exit(1)
# Postgresql connection
try:
#cnx_psql = get_connection(get_global_config(), 'pg_dwh')
cnx_psql =psycopg2.connect(host='xxx.xxx.xx.xx',
dbname='postgres',
port='5432',
user='postgres',
password='*********')
print('PSQL DB connected')
except psycopg2.Error as e:
print('PSQL: Unable to connect!\n{0}').format(e)
sys.exit(1)
# Cursors initializations
cur_msql = cnx_msql.cursor()
cur_psql = cnx_psql.cursor()
SQL_test="""SELECT count(*) from action_status;"""
cur_msql.execute(SQL_test)
records = cur_msql.fetchall()
for row in records:
print("count = ", row[0], )
msql_command=""
try:
SQL_load="""SELECT created_at,payload from staging.sync;"""
msql_ins="""INSERT INTO action_status(payload,created_at)VALUES (%s, %s) """
cur_psql.execute(SQL_load)
for row in cur_psql:
try:
print(row[0])
print(row[1])
cur_msql.execute(msql_ins, row[0],row[1])
except psycopg2.Error as e:
print('ffffffffffffff')
print ("Cannot execute the query!!", e.pgerror)
sys.exit(1)
cnx_msql.commit()
cur_msql.execute(SQL_test)
records = cur_msql.fetchall()
for row in records:
print("count = ", row[0], )
except (Exception, psycopg2.Error) as error:
print ("Error while fetching data from PostgreSQL", error)
finally:
## Closing cursors
cur_msql.close()
cur_psql.close()
## Committing
cnx_psql.commit()
## Closing database connections
cnx_msql.close()
cnx_psql.close()
if __name__ == '__main__':
db_connect()
msql\u ins
缺少分号(不确定是否需要)。更重要的是,您缺少一个元组;而不是:
cur_msql.execute(msql_ins, row[0],row[1])
请尝试以下方法:
cur_msql.execute(msql_ins, (row[0], row[1]))
希望有帮助:)您是否从postgresql获取数据??请先检查一下。。。然后,在mysql中插入每条记录时,确保您提交到游标。@AkashPagar是的,我从postgreSQL获取数据,但它没有插入到mysql,我在for循环后提交了它。这很好,对吗?@Akashpagar您是否发现任何错误或任何可以改进的地方?您是否查看了mysql日志以查看您的查询发送到mysql服务器的内容?@nbk我无法访问该服务器。但可能我可以请人检查一下。您是否发现其他错误?