【发布时间】:2014-08-06 00:16:02
【问题描述】:
所以基本上我正在研究这个从 sqlite 文件中读取数据并将其与 MySQL 服务器同步(上传数据)的 python 脚本。该脚本似乎很好,因为我没有收到任何错误,但每当我在 phpMyAdmin 中查看更改时,什么都没有发生。
这是我的完整代码:
#!/usr/bin/python2.7
__author__ = ''
import sys
import sqlite3 as lite
import MySQLdb as mysql
c=0
liteDB = ''
for arg in sys.argv:
c=c+1
if c==2:
liteDB = arg
SQLServer = '*********'
SQLUser = '***********'
SQLPass = '***********'
SQLDB = '*************'
try:
conMSQL = mysql.connect(SQLServer, SQLUser, SQLPass, SQLDB)
curMSQL = conMSQL.cursor()
try:
conLite = lite.connect(liteDB)
curLite = conLite.cursor()
curLite.execute('SELECT * FROM Ventas')
while True:
row=curLite.fetchone()
if row == None:
break
texto = 'INSERT INTO Ventas VALUES (' + str(row[0]) + ',' + str(row[1]) + ',' + str(row[2]) + ',' + str(row[4]) + ',' + str(row[3]) + ',' + str(row[5]) + ',' + str(row[6]) + ',' + str(row[8]) + ',' + str(row[7]) + ',' + str(row[9]) + ',' + str(row[10]) + """,'""" + row[11] + """')"""
print texto
curMSQL.execute('INSERT INTO Ventas VALUES (' + str(row[0]) + ',' + str(row[1]) + ',' + str(row[2]) + ',' + str(row[4]) + ',' + str(row[3]) + ',' + str(row[5]) + ',' + str(row[6]) + ',' + str(row[8]) + ',' + str(row[7]) + ',' + str(row[9]) + ',' + str(row[10]) + """,'""" + row[11] + """');""")
conLite.commit()
except lite.Error, e:
if conLite:
conLite.rollback()
print "Error %s:" % e.args[0]
finally:
if conLite:
conLite.close()
except mysql.Error, e:
print "Error MySQL %s:" % e.args[0]
sys.exit(1)
finally:
if conMSQL:
conMSQL.close()
这是我获取 sqlite 数据并将其插入 MySQL 服务器的特定部分
while True:
row=curLite.fetchone()
if row == None:
break
texto = 'INSERT INTO Ventas VALUES (' + str(row[0]) + ',' + str(row[1]) + ',' + str(row[2]) + ',' + str(row[4]) + ',' + str(row[3]) + ',' + str(row[5]) + ',' + str(row[6]) + ',' + str(row[8]) + ',' + str(row[7]) + ',' + str(row[9]) + ',' + str(row[10]) + """,'""" + row[11] + """')"""
print texto
curMSQL.execute('INSERT INTO Ventas VALUES (' + str(row[0]) + ',' + str(row[1]) + ',' + str(row[2]) + ',' + str(row[4]) + ',' + str(row[3]) + ',' + str(row[5]) + ',' + str(row[6]) + ',' + str(row[8]) + ',' + str(row[7]) + ',' + str(row[9]) + ',' + str(row[10]) + """,'""" + row[11] + """');""")
conLite.commit()
我是 python 新手,因此感谢任何帮助,在此先感谢!
【问题讨论】:
-
您的代码容易受到 SQL 注入的攻击。见:bobby-tables.com/python.html
标签: python mysql sqlite python-2.7