Frage

I HAVE ADDED MY OWN ANSWER THAT WORKS BUT OPEN TO IMPROVEMENTS

After seeing a project at datanitro. I took on getting a connection to MySQL (they use SQLite) and I was able to import a small test table into Excel from MySQL.

Inserting new updated data from the Excel sheet was this next task and so far I can get one row to work like so...

import MySQLdb

db = MySQLdb.connect("xxx","xxx","xxx","xxx")
c = db.cursor()

c.execute("""INSERT INTO users (id, username, password, userid, fname, lname)
VALUES (%s, %s, %s, %s, %s, %s);""",
      (Cell(5,1).value,Cell(5,2).value,Cell(5,3).value,Cell(5,4).value,Cell(5,5).value,Cell(5,6).value,))

db.commit()
db.close()

...but attempts at multiple rows will fail. I suspect either issues while traversing rows in Excel. Here is what I have so far...

import MySQLdb

db = MySQLdb.connect(host="xxx.com", user="xxx", passwd="xxx", db="xxx")
c = db.cursor()

c.execute("select * from users")
usersss = c.fetchall()

updates = []

row = 2 # starting row

while True:
    data = tuple(CellRange((row,1),(row,6)).value)
    if data[0]:
        if data not in usersss: # new record
            updates.append(data)
        row += 1
    else: # end of table
        break

 c.executemany("""INSERT INTO users (id, username, password, userid, fname, lname) VALUES (%s, %s, %s, %s, %s, %s)""", updates)

db.commit()
db.close()

...as of now, I don't get any errors, but my new line is not added (id 3). This is what my table looks like in Excel...

enter image description here

The database holds the same structure, minus id 3. There has to be a simpler way to traverse the rows and pull the unique content for INSERT, but after 6 hours trying different things (and 2 new Python books) I am going to ask for help.

If I run either...

print '[%s]' % ', '.join(map(str, updates))

or

print updates

my result is

[]

So this is likely not passing any data to MySQL in the first place.

LATEST UPDATE AND WORKING SCRIPT

Not exactly what I want, but this has worked for me...

c = db.cursor()
row = 2
while Cell(row,1).value != None:
    c.execute("""INSERT IGNORE INTO users (id, username, password, userid, fname, lname)
    VALUES (%s, %s, %s, %s, %s, %s);""",
      (CellRange((row,1),(row,6)).value))
    row = row + 1
War es hilfreich?

Lösung 2

I ended up finding a solution that gets me an Insert on new and allows for UPDATE of those that are changed. Not exactly a Python selection based on a single query, but will do.

import MySQLdb

db = MySQLdb.connect("xxx","xxx","xxx","xxx")
c = db.cursor()
row = 2
while Cell(row,1).value is not None:
    c.execute("INSERT INTO users (id, username, password, \
    userid, fname, lname) \
    VALUES (%s, %s, %s, %s, %s, %s) \
    ON DUPLICATE KEY UPDATE \
    id=VALUES(id), username=VALUES(username), password=VALUES(password), \
    userid=VALUES(userid), fname=VALUES(fname), lname=VALUES(lname);",
    (CellRange((row,1),(row,6)).value))

    row = row + 1

db.commit()   
db.close()

Andere Tipps

Here is your problem:

while True:
    if data[0]:
        ...
    else:
        break

Your first id is 0, so in the first iteration of the loop data[0] will be falsely and your loop will exit, without ever adding any data. What you probably ment is:

while True:
    if data[0] is not None:
        ...
    else:
        break
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top