您好,我用外键连接了两个MySql表,我想通过python向它们插入数据。这是一段有效的代码,但是应该有一个替代的和专业的方法来这样做,否则我不需要外键,我只插入第二个表的第一个表customer_ID列的ID。谢谢你的帮助。
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_id()
获取最后插入的id/客户的id呃。。。^{} 。在
相关问题 更多 >
编程相关推荐