こんにちは、2 つの MySql テーブルを外部キーで接続し、Python を介してそれらにデータを挿入したいと考えています。これは機能するコードですが、代替の専門的な方法があるはずです。それ以外の場合は、外部キーは必要なく、最初のテーブルのIDを挿入するだけです。 customer_id 2番目のテーブルの列。助けてくれてありがとう。
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")