Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和MySql,将数据插入到两个表中的替代方法,这两个表通过for外键连接_Python_Mysql_Insert - Fatal编程技术网

Python和MySql,将数据插入到两个表中的替代方法,这两个表通过for外键连接

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',

您好,我用外键连接了两个MySql表,我想通过python将数据插入其中。这是一段有效的代码,但应该有一种替代的专业方法来实现,否则我不需要外键,只需插入第二个表的第一个表customer\u 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\u id()
获取上次插入的id/customer\u id

使用
db.insert\u id()
获取上次插入的id/customer\u id

错误

从专业角度来说,我会使用一个框架。从专业角度来说,我会使用一个框架。谢谢。我也喜欢这种方式,节省了几行代码。甚至还不知道外键有什么好处。谢谢。我也喜欢这种方式,节省了几行代码。甚至连外键的好处都不知道。