Python,Mysql,插入NULL [英] Python, Mysql, insert NULL

查看:400
本文介绍了Python,Mysql,插入NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.4

MySQL-python.exe-1.2.0.win32-py2.4.zip


如何插入NULL值一个表(MySQL数据库)。

我不能将var设置为NULL?或者还有其他可能吗?

我的var必须是变量字符串或NULL。

Becaus我有一个if语句:

如果.. ..

cursor.execute(" ................. insert NULL .............。" ;)

if ....

cursor.execute(" ................. insert" string" ; ..............")

Python 2.4
MySQL-python.exe-1.2.0.win32-py2.4.zip

How can I insert a NULL value in a table (MySQL-database).
I can''t set a var to NULL? Or is there a other possibility?
My var must be variable string or NULL.
Becaus i have a if statement:
if ....
cursor.execute(".................insert NULL ..............")
if ....
cursor.execute(".................insert "string" ..............")

推荐答案

Python_it写道:
Python_it wrote:
Python 2.4
MySQL-python.exe-1.2.0.win32-py2.4.zip

如何在表中插入NULL值( MySQL数据库)。
我不能将var设置为NULL吗?或者还有其他可能性吗?
我的var必须是变量字符串或NULL。
Becaus我有一个if语句:
if ....
cursor.execute(" .................插入NULL ..............")
如果....
光标.execute(" ................. insert" string" ..............")
Python 2.4
MySQL-python.exe-1.2.0.win32-py2.4.zip

How can I insert a NULL value in a table (MySQL-database).
I can''t set a var to NULL? Or is there a other possibility?
My var must be variable string or NULL.
Becaus i have a if statement:
if ....
cursor.execute(".................insert NULL ..............")
if ....
cursor.execute(".................insert "string" ..............")



使用参数!例如,您是否尝试过:


cursor.execute(" insert into tablename(fieldname)values(%s)",[value])

>
没有将被转换为NULL,任何其他值将被引用为必要的。


BTW,你没有写你正在使用哪个驱动程序。

每个驱动程序的参数用法都不同,但它是标准化的。

如果它与DB API 2.0兼容,那么参数化查询应该在PEP中被视为

desicribed in PEP 0249:

http:// www。 python.org/peps/pep-0249.html


在'光标对象''部分下,查找''.execute''方法。


Use parameters! For example, did you try:

cursor.execute(" insert into tablename(fieldname) values (%s)",[value])

None will be converted to NULL, any other value will be quoted as neccesary.

BTW, you did not write which driver are you using.
Usage of parameters is different for each driver, but it is standardized.
If it is DB API 2.0 compatible, then parametrized queries should work as
desicribed in PEP 0249:

http://www.python.org/peps/pep-0249.html

Under ''cursor objects'' section, look for the ''.execute'' method.



顺便说一下,你没有写你正在使用哪个驱动程序。

BTW, you did not write which driver are you using.




哦,你做到了。抱歉。 :-(导入你的数据库模块''你的模块'然后


打印yourmodule.paramstyle


paramstyle的描述也在PEP249中:


paramstyle


表示参数标记类型的字符串常量

接口预期的格式。可能的值是

[2]:


''qmark''问号风格,

例如''... WHERE name = ?''

''数字''数字,位置风格,

例如''... WHERE name =:1''

''命名''命名样式,

例如''... WHERE name =:name''

''format''ANSI C printf格式代码,

例如''... WHERE name =%s''

''pyformat''Python扩展格式代码,

例如''.. .WHERE name =%(name)s''

Best,


Les


eg''。 ..WHERE name =%(name)s''



Oh, you did. Sorry. :-( Import your DB module ''yourmodule'' and then

print yourmodule.paramstyle

Description of paramstyle is also in PEP249:

paramstyle

String constant stating the type of parameter marker
formatting expected by the interface. Possible values are
[2]:

''qmark'' Question mark style,
e.g. ''...WHERE name=?''
''numeric'' Numeric, positional style,
e.g. ''...WHERE name=:1''
''named'' Named style,
e.g. ''...WHERE name=:name''
''format'' ANSI C printf format codes,
e.g. ''...WHERE name=%s''
''pyformat'' Python extended format codes,
e.g. ''...WHERE name=%(name)s''
Best,

Les

e.g. ''...WHERE name=%(name)s''


我知道如何在数据库中插入值。

那不是我的问题!

我的问题是我如何插入NULL de mysql-database中的值。

没有Python中的een对象而NULL不是。

没有未转换为NULL?

表显示无,不是NULL!

I know how to insert values in a database.
That''s not my problem!
My problem is how i insert NULL values in de mysql-database.
None is een object in Python and NULL not.
None is not converted to NULL?
Table shows None and not NULL!


这篇关于Python,Mysql,插入NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆