【问题标题】:No errors on my Python script when communicating to MySQL, still no changes与 MySQL 通信时,我的 Python 脚本没有错误,仍然没有变化
【发布时间】: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 新手,因此感谢任何帮助,在此先感谢!

【问题讨论】:

标签: python mysql sqlite python-2.7


【解决方案1】:

你需要 commit() mysql 数据库,因为你插入的是它,而不是 sqlite 数据库。编辑:是的,您绝对应该阅读有关如何参数化查询而不是将字符串连接在一起的信息,它甚至没有被转义。

【讨论】:

    猜你喜欢
    • 2012-04-23
    • 2019-04-06
    • 1970-01-01
    • 2015-01-17
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多