我正在尝试从 python 向 mysql 表添加一个值。我已经尝试了大约五种不同的方法,但仍然收到错误消息,指出当我调用插入时,表中的其他键没有传递值。问题是我不想插入任何其他值,只想插入一个特定的键。这是我目前拥有的代码,提前感谢您的帮助
cursor.execute("SELECT * FROM SONG") result = cursor.fetchall() sname = str(input("What is the name of the song you are looking for: ")) for row in result: title = row[0] if(title == sname): sql = ("INSERT INTO USER (s_id) VALUES (%s)") val = row[1] cursor.execute(sql, (val,))
mysql.connector.errors.DatabaseError:1364(HY000):字段“userid”没有 默认值
CREATE TABLE USER (username VARCHAR(20), userid CHAR(3), a_id CHAR(3), s_id CHAR(3), PRIMARY KEY (userid), FOREIGN KEY (a_id) REFERENCES ALBUM(a_id), FOREIGN KEY (s_id) REFERENCES SONG(s_id)); CREATE TABLE PLAYLIST (p_name VARCHAR(20), p_id CHAR(3) NOT NULL, userid CHAR(3), s_id CHAR(3), PRIMARY KEY (p_id), FOREIGN KEY (userid) REFERENCES USER(userid), FOREIGN KEY (s_id) REFERENCES SONG(s_id));
正确的方法是不使用 python,而只使用 MySQL 查询
https://www.w3schools.com/sql/sql_insert_into_select.asp