2

I have used namedtuples to read rows from mySQL ... it is great. Can I use the same mechanism to INSERT rows into mySQL. My system is changing quite a bit, so I need an simple way to map python values to columns in the database, without having to check the order of the values/columns every time I change my program I am using mySQLDB library - but could change

4

1 回答 1

2

用于 INSERT 查询的 namedtuple 的行为就像普通元组一样,您必须保持列/值的顺序。您可以使用命名占位符和字典来避免这种情况。

cursor.execute("INSERT INTO person (name, age) VALUES (%(name)s, %(age)s)", {"name": "Bernard", "age": 30})

相关问题:Python MySQLdb: Query parameters as a named dictionary

于 2013-08-09T20:52:22.510 回答