English 中文(简体)
pysqlite: 1. 替换栏目或表格名称的持有人?
原标题:pysqlite: Placeholder substitution for column or table names?

Using pysqlite I am making a procedure to do something with some data. The same kind of operation is done on similar fields in multiple tables and columns, so I thought I could parameterize the sql statement as shown below:

def foo():
  column =  c 
  table =  t 
  row = 1
  # preferred approach, gives syntax error
  c.execute( SELECT ? FROM ? WHERE id=? , (column, table, row))
  # sanity check, works fine
  c.execute( SELECT c FROM t WHERE id=? , (row))
  # workaround, also works, but is this the right way?
  c.execute( SELECT % FROM % WHERE id=?  % (column, table), row))

我发现的错误并不非常有用(sqlite3.Operationalalal 错误:接近“?”: syntax误差,但我看上去:Pysqlite并不欣赏以这种方式使用土地持有人。

谁能指出,在这样做的同时,还有哪些进展?

最佳回答

您根本不能使用排位持有人来填写专栏或表格。 我对此没有权威的引言——我“知道”,这只是从审判和失败中来。 它具有某种意义:

  • If the columns and table could be parametrized, there would be little purpose to preparing (execute-ing) the SQL statement before fetching, since all parts of the statement could be replaced.
  • I m not sure about pysqlite1, but MySQLdb automatically quotes all string parameters. Column and table names should not be quoted. So it would complicate the parsing required by the driver if it had to decide if a placeholder represented a column or table name versus a value that needs quoting.

简言之,你找到了正确的方式——使用扼制格式。

c.execute( SELECT {} FROM {} WHERE id=? .format(column, table), row))

<>1> 并非所有驾驶员都引用参数——<代码>oursqltt. ,因为它分别向服务器发送了文件卡和论点。

问题回答

@unutbu回答说,没有办法让持单人查阅表/栏。 我建议你现在做些什么,但也要引用表格名称来保护自己不受可能有奇名的表或栏的影响。

What does the SQL Standard say about usage of backtick(`)? already explains this to some extent, and in spite of the opinion in that answer, I would say that in your case, quoting is a good idea.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签