简体   繁体   中英

How to insert multi-row to mysql?

I have a table "control" like this in local mysql (IP = 192.168.137.165)

Card_ID     Time_in      Time_out     Index
  101         7:00         11:00       0
  102         7:15         11:00       1
  103         6:45         12:00       1

Local and remote tables "control" are the same table

How to select all rows with index = 1 and insert to remote mysql using python? (IP=192.168.137.1)

I have ideas for backup database. If remote mysql alive, index = 0 and if remote mysql die, index = 1.

And if remote re-connect, I want to send data in local mysql to remote.So I want to just want to insert the line with index = 1

Code connect and insert mysql

import MySQLdb
db_local = MySQLdb.connect("localhost","root","loot","luan_van")
db = MySQLdb.connect("192.168.137.1","root_a","","luan_van")
def add_remote():
   with db:
       cur = db.cursor()
       cur.execute("INSERT INTO control(Card_ID,Time_in) VALUES ('%d', NOW())" %num)
       return

I can insert simple but I don't know insert multi-rows. Please help me. Thanks.

does this answer your question?

Inserting multiple rows in mysql

which will be something like this:

cur.execute("INSERT INTO control (Card_ID,Time_in) 
    VALUES('%d', NOW()),('%d', NOW()),('%d', NOW())", %num1, %num2, %num3)    

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