Python insert value to foreign key attribute (MYSQL) -
i created 2 table in mysql database
staff table:
+----+-----------+------------- | id | name | +----+-----------+------------- | no data available in table | +----+-----------+-------------
qualification table:
+----+----------+--------------+ | id | title | staff_id | +----+----------+--------------+ | no data available in table | +----+--------------+-----------
id of both table primary key , staff_id of qualification table foreign key. python code like
con=pymysql.connect(host='localhost',port=3306,user='root',password='admin',db='mydb') cursor=con.cursor() cursor.execute("insert staff (id,name)values (%s,%s)",(1001,"mr tan")) cursor.execute("insert qualification values (%s,%s,%s)",(2001,"professor","select id staff id=1001")) con.commit() cursor.close() con.close()
i try insert value foreign key attribute "typeerror: not arguments converted during string formatting"
why executing select
statement. have id available. pass such:
con = pymysql.connect(host='localhost',port=3306,user='root',password='admin',db='mydb') cursor = con.cursor() cursor.execute( "insert staff (id,name) values (%d,%s)", (1001,"mr tan") ) cursor.execute( "insert qualification values (%d,%s,%d)", (2001,"professor",1001) )
Comments
Post a Comment