简体   繁体   English

如何在MySQL中插入多行?

[英]How to insert multi-row to mysql?

I have a table "control" like this in local mysql (IP = 192.168.137.165) 我在本地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? 如何选择索引= 1的所有行并使用python插入远程mysql? (IP=192.168.137.1) (IP = 192.168.137.1)

I have ideas for backup database. 我对备份数据库有想法。 If remote mysql alive, index = 0 and if remote mysql die, index = 1. 如果远程mysql存活,则索引= 0;如果远程mysql死亡,则索引= 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 如果远程重新连接,我想将本地mysql中的数据发送到remote.So,我只想插入index = 1的行

Code connect and insert mysql 代码连接并插入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 在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)    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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