简体   繁体   English

是什么? 在python pyodbc模块中的意思是

[英]what does ? mean in python pyodbc module

import pyodbc
cursor.execute("INSERT INTO Testing_Param(Seed_Number,Cycle_Name) VALUES (?,?)",('0','CoupleIn'))

what does the "?" 什么是“?” mean in the code? 在代码中意味着什么? When I try to replace the ? 当我尝试更换时? to %s for the "CoupleIn" which is the string and %d for the "0", why does it appear error message: %s表示字符串“ CoupleIn”,%d表示“ 0”,为什么会出现错误消息:

pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 2 parameters were supplied', 'HY000') pyodbc.ProgrammingError:('SQL包含0个参数标记,但提供了2个参数','HY000')

I am new to the pyodbc module to do transfering data from Python into Microsoft SQL server 我是pyodbc模块的新手,可以将数据从Python传输到Microsoft SQL Server

? is the placeholder for the substitution engine. 是替代引擎的占位符。 The cursor.execute function is responsible for properly escaping the values in the tuple and inserting them into the query where the respective question marks are to form a valid query. cursor.execute函数负责正确地将元组中的值转义并将其插入到查询中,其中各个问号将形成有效的查询。 This keeps you safe from sql injection attacks where normal string interpolation would leave your database vulnerable to attackers. 这样可以使您免受SQL注入攻击的攻击 ,在常规注入中 ,普通的字符串插值会使数据库容易受到攻击者的攻击。

You can read more about the standard python database apis in PEP-0249 -- Specifically, your database wrapper is using qmark paramstyle. 您可以在PEP-0249中阅读有关标准python数据库api的更多信息-具体来说,您的数据库包装器正在使用qmark paramstyle。

The two question marks are placeholders for the parameters 0 and CoupleIn , respectively. 这两个问号分别是参数0CoupleIn占位符。 This is similar for the text formatting in Python where the placeholder for a variable is % . 这与Python中的文本格式相似,其中变量的占位符为%

See http://mkleehammer.github.io/pyodbc/ under the paragraph Parameters 请参阅“ Parameters ”段落下的http://mkleehammer.github.io/pyodbc/

It is a place holder for parameter values, '0' and 'Couple'. 它是参数值“ 0”和“ Couple”的占位符。 cursor.execute will substitute the values in place of ?s. cursor.execute将代替?s的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM