简体   繁体   中英

update multiple rows with different values python and mysql

i want to update multiple rows with different values.

for example my table is :

id | Name | Data

1 |ahmad | year 2015

2 | ali | year 2014

i want my table to look like this:

id | Name | Data | number

1 |ahmad | year 2015 | 2

2 | ali | year 2014 | 3

There are more than 7000 rows i can't fill them one by one.

The query i am trying to do so in python is:

 #words count in a tweet
    words= processedRow.split()
    fdist2=len(words)

    for val in cursor.fetchall():
        cursor.execute("""UPDATE TWEETER1 SET t1=%s where id = %s""",(fdist2,val))
        db.commit()

Kindly help me in doing my work. Thank you

Yes, you can. 7000 is a low number for a computer.

You could try to optimise it for >100k to use multiple database connections or not to commit on each update but that's outside the scope of your questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM