0

So basically I'm working on this python script that reads data from an sqlite file and synchronizes it with a MySQL server (uploads the data). The script seems to be fine since I get no errors, still whenever I look in phpMyAdmin for the changes nothing happens.

Here's my complete code:

#!/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()

and here is the specific part where I grab the sqlite data and Insert it into the MySQL server

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()

I'm new to python so any help is appreciated, thanks in advance!

Jesus M Lopez F
  • 11
  • 1
  • 1
  • 5
  • possible duplicate of [Database does not update automatically with MySQL and Python](http://stackoverflow.com/questions/384228/database-does-not-update-automatically-with-mysql-and-python) – univerio Aug 06 '14 at 00:20
  • 2
    Your code is vulnerable to SQL injections. See: http://bobby-tables.com/python.html – Karol S Aug 06 '14 at 00:23

1 Answers1

2

You need to commit() the mysql database, since you're inserting into it, not the sqlite database. Edit: and yes, you should definitely read about how to parametrize your queries and not concatenate strings together, it's not even being escaped.

Jason S
  • 13,538
  • 2
  • 37
  • 42