如何使用mySQLDB字符串格式插入默认值?
我现在已经遇到了几次,在那里我尝试使用value
插入(或批量插入)在MySQL表中,而无需明确定义列,但是我想有一个自动_increment字段。令auto_increment或我无法插入值的生成列。
具体来说,假设我有一个带有三列的表,两个用于数字的列,一个是这些数字的总和:
CREATE TABLE `addition` (
`num_1` int DEFAULT NULL,
`num_2` int DEFAULT NULL,
`sum` int GENERATED ALWAYS AS ((`num_1` + `num_2`)) VIRTUAL
)
如果我想使用mySQLDB Cursor对象cur
i插入该数据库,则不能做:
cur.execute('插入添加值%s',[(2,2,'默认值')])
...因为您无法定义该值的值生成的字段“ sum”和'默认值'
在此解释为文字字符串。您将获得MySQL错误3105:不允许为生成的列“总和”中指定的值。
但是,对于任何值,我都可以认为将'默认值'
发生同样的错误,例如none
或false
。
因此,是否可以在数据部分中传递一个值(即[(2,2,< somets>)]
)告诉mySQL使用sum
sum < /代码>列?或者是在SQL本身中定义它的唯一方法,即
cur.execute('插入添加值(%s,%s,默认值)',[2,2]),[2,2])
这将是当表结构不知道或容易更改时,您不想硬编码哪些字段应插入默认值时有用。
-edit-
如果我要尝试cur.execute('插入添加值%s',[(2,2,'default'))),这试图插入字符串
'默认值'
,同样是none
或我想到的任何其他值。因此,问题确实是一个问题,我可以通过我可以通过的字段(例如mysqldb.default()
)来实现这一目标。因此,最终结果看起来像cur.execute('插入添加值%s',[(2,2,mySqldB.default())]
>
I have run into this a few times now, where I'm trying to insert (or bulk insert) into a MySQL table using VALUES
without defining the columns explicitly, but there is an auto_increment field I want to let auto_increment, or a generated column that I can't insert a value for.
Specifically, let's say I have a table with three columns, two for numbers and one generated column that's the sum of those numbers:
CREATE TABLE `addition` (
`num_1` int DEFAULT NULL,
`num_2` int DEFAULT NULL,
`sum` int GENERATED ALWAYS AS ((`num_1` + `num_2`)) VIRTUAL
)
If I want to insert values to this database with a MySQLdb cursor object cur
, I can't do:
cur.execute('INSERT INTO addition VALUES %s', [(2, 2, 'DEFAULT')])
...because you can't define the value for the generated field "sum", and 'DEFAULT'
here is interpreted as the literal string. You'll get MySQL error 3105: The value specified for generated column 'sum' in table 'addition' is not allowed.
But the same error occurs for any value I could think to put in place of 'DEFAULT'
, for example None
or False
.
So is there any way to pass a value in the data section (i.e. [(2, 2, <something>)]
) to tell MySQL to use the default value for the sum
column? Or is the only way to define it in the SQL itself, i.e.
cur.execute('INSERT INTO addition VALUES (%s, %s, DEFAULT)', [2, 2])
This would be helpful when the table structure isn't known, or is prone to change, and you don't want to hard-code which fields should insert as DEFAULT.
--Edit--
Some clarification post-discussion in the comments, if I were to try cur.execute('INSERT INTO addition VALUES %s', [(2, 2, 'DEFAULT')])
, this tries to insert the literal string 'DEFAULT'
, similarly for None
, or any other value I could think of. So the question is really a Python question, is there a field (e.g. MySQLdb.DEFAULT()
) that I can pass to accomplish this. So the final result would look something like cur.execute('INSERT INTO addition VALUES %s', [(2, 2, MySQLdb.DEFAULT())])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要计算的总和列,而不是通过生成的列在数据库侧处理它:
然后,当您插入两个数字时,MySQL将为您处理数学:
请注意,MySQL中生成的列默认是虚拟的,这意味着该列是总和实际上不会持续存在,而是在您进行选择时会发生。
If you want a computed sum column, than handle it on the database side via a generated column:
Then, when you insert two numbers, MySQL will handle the math for you:
Note that generated columns in MySQL are virtual by default, meaning that the sum won't actually be persisted, by rather would happen at the time you do a select.