English 中文(简体)
Sqlite db, 多次更新:处理案文和浮标
原标题:Sqlite db, multiple row updates: handling text and floats

我拥有一个大型的Kingk数据库,其中包含其他栏目(var1......var 50)的案文和抽签。 其中多数是数字,但有些是文字。

我正试图从数据库中提取数据,用 p子处理数据,并重写数据——我需要为布局的所有各行都这样做。

至今,工作类型如下:

# get row using select and process
fields = ( , .join(keys)) # "var1, var2, var3 ... var50"
results =  , .join([results[key] for key in keys]) # "value for var1, ... value for var50"
cur.execute( INSERT OR REPLACE INTO results (id, %s) VALUES (%s, %s);  %(fields, id, results))

然而,这 null倒了我没有明确补充的栏目。 我可以通过重写这部法典来加以确定,但是这感觉到很不舒服,因为我不得不用扼杀性分类和改写数据(即一栏不变)。

很明显,掌握最新消息的方法就是这样:

update table set var1 = 4, var2 = 5, var3="some text" where id = 666;

简言之,我的道路是操作<条码>>><>>>>>>,并添加<条码>=标记(但我如何适当引用所有成果(因为我不得不引用案文领域,而且可能也包含其中的引述)?

我很混淆。 任何要点都将非常有益。

感谢!

最佳回答

正如其他人强调的那样,使用贬损性的论点。 这里是你在拥有数量可变的钥匙时如何构造这份清单的例子:

sql=( UPDATE results SET  
     +  ,  .join(key+  = ?  for key in keys)
     +  WHERE id = ? )
args = [results[key] for key in keys] + [id]
cur.execute(sql,args)
问题回答

你们不必关心像引言等事情,事实上,你们应该这样做。 如果你这样做的话,它不仅更加方便,而且还要关注被称为“q”的安全问题:

sql = "update table set var1=%s, var2=%s, var3=%s where id=666" 
cursor.execute(sql, (4, 5, "some text"))

这里的关键要点是,第二次发言中的 s和价值没有被“%”分开,而是被“,”——这并不是一种扼杀的操纵,而是你对执行职能、实际q和价值提出了两个论点。 每一百分比由价值图的数值取代。 然后,数据库的驱动人知道如何照顾各种价值。

可以用同样的方式重新表述声明,尽管我不敢肯定,而且现在可以检验你是否可以取代实地名称(你在插入——ql说明中的第一 %)。

为了回头看你的总体问题,你可以放弃你的价值观,积极增加“,%d=%”,即“一”变量,同时在清单中增加实际价值。





相关问题
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 ]="...

热门标签