Python和MySql,将数据插入到两个表中的替代方法,这两个表通过for外键连接
您好,我用外键连接了两个MySql表,我想通过python将数据插入其中。这是一段有效的代码,但应该有一种替代的专业方法来实现,否则我不需要外键,只需插入第二个表的第一个表customer\u ID列的ID。谢谢你的帮助。Python和MySql,将数据插入到两个表中的替代方法,这两个表通过for外键连接,python,mysql,insert,Python,Mysql,Insert,您好,我用外键连接了两个MySql表,我想通过python将数据插入其中。这是一段有效的代码,但应该有一种替代的专业方法来实现,否则我不需要外键,只需插入第二个表的第一个表customer\u ID列的ID。谢谢你的帮助。 Product = str(self.Text.GetValue()) Product2 = str(self.Description.GetValue()) db=MySQLdb.connect('127.0.0.1', 'root',
Product = str(self.Text.GetValue())
Product2 = str(self.Description.GetValue())
db=MySQLdb.connect('127.0.0.1', 'root','password', 'database')
cursor = db.cursor()
cursor.execute("INSERT INTO customer (Address) VALUES (%s)", (Product))
cursor.execute("SELECT id FROM customer ORDER BY id DESC LIMIT 1")
rows = cursor.fetchall()
the_id= rows[0][0]
cursor.execute("INSERT INTO product_order (customer_id, description) VALUES (%s,%s)", (the_id,Product2))
cursor.execute("commit")
使用
db.insert\u id()
获取上次插入的id/customer\u id使用db.insert\u id()
获取上次插入的id/customer\u id错误 呃 从专业角度来说,我会使用一个框架。从专业角度来说,我会使用一个框架。谢谢。我也喜欢这种方式,节省了几行代码。甚至还不知道外键有什么好处。谢谢。我也喜欢这种方式,节省了几行代码。甚至连外键的好处都不知道。