Alternatively, by using a "dummy" default value, you can do it in two steps (just be careful not to leave any "dummy" values floating around, or use values that are meaningful/well-documented):将列添加为NOT NULL DEFAULT ''(或将0用于数字类型)在每一行中填充值Add column as NOT NULL DEFAULT '' (or use e.g. 0 for numeric types)Populate column with values in every row您可以选择再次更改表格以删除DEFAULT值.就我个人而言,我更喜欢第一种方法,因为它不会在表中引入无意义的值,并且如果第二步有问题,则更有可能引发错误.当列适合某个自然的DEFAULT值时,我 可能会使用第二种方法,并且我打算将其保留在最终表定义中.You can optionally alter the table again to remove the DEFAULT value. Personally, I prefer the first method because it doesn't introduce meaningless values into your table and it's more likely to throw an error if the second step has a problem. I might go with the second method when a column lends itself to a certain natural DEFAULT value and I plan to keep that in the final table definition.此外,您没有正确地设置查询参数;您应该将参数值传递给方法,而不是在方法调用中格式化字符串参数.换句话说:Additionally, you are not parameterizing your query correctly; you should pass the parameter values to the method rather than formatting the string argument inside the method call. In other words:cursor.execute("Query with %s, %s, ...", iterable_with_values) # Do this!cursor.execute("Query with %s, %s, ..." % iterable_with_values) # NOT this! 这篇关于无法将 pandas 数据框中的列添加到python中的mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 10:02