sqlite3如果不存在则插入(使用Python) [英] sqlite3 INSERT IF NOT EXIST (with Python)

查看:565
本文介绍了sqlite3如果不存在则插入(使用Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我真的是个超级新手,所以我希望我能够正确地发布问题。请告诉我是否有问题。

First of all, I am really super-new so I hope that I will be able to post the question correctly. Please tell me if there is any problem.

现在,这是我的问题:我想用数据填充数据库,前提是该数据尚不存在在里面。我搜索了这个主题,我想我找到了正确的答案(您可以在此处阅读一个示例:[在SQLite中插入如果不存在语句),但是我需要在python中编写这些简单的命令行..这就是我的问题。 (我预计我也是Python的新手)

Now, here is my question: I would like to fill a database with a data, only if it doesn't already exist in it. I searched for this topic and I think I found correct the answer (you can read one example here: ["Insert if not exists" statement in SQLite) but I need to write these simple command line in python.. and that's my problem. (I anticipate that I am quite new in Python too)

所以,这就是我所做的:

So, here is what I did:

    self.cur.execute("INSERT INTO ProSolut VALUES('a','b','c')")
    self.cur.execute("SELECT * FROM ProSolut")
    self.cur.execute("WHERE NOT EXISTS (SELECT * FROM ProSolut WHERE VALUES = ('a','b','c'))")

,这是错误:

[ERROR] behavior.box :_safeCallOfUserMethod:125 _Behavior__lastUploadedChoregrapheBehaviorbehavior_1142022496:/ProSolutDB_11: Traceback (most recent call last):   File "/usr/lib/python2.7/site-packages/albehavior.py", line 113, in _safeCallOfUserMethod     func(functionArg)   File "<string>", line 45, in onInput_onStart OperationalError: near "WHERE": syntax error  

所以基本上我认为第三个字符串中的括号(有问题。->( OperationalError:在 WHERE附近:语法错误)

so basically I think there is some problem with the bracket "(" in the 3rd string. --> ("OperationalError: near "WHERE": syntax error")

我知道可能性y这是一个愚蠢的错误。
如果您能帮助我,我将不胜感激。

I know that probably it's a stupid error. If you can help me, I would really appreciate.

非常感谢

EG:我忘了说我正在使用软件Choregraphe ,该软件使用Python语言来构造所有功能块。
这意味着,即使语言基本上是Python,有时语义也不尽相同。
我希望这篇文章将来对您有所帮助。

E.G.: I forgot to say that I am using the software Choregraphe, which uses the Python language to construct all the functional blocks. That means that, even if the language is basically Python, sometimes the semantic is not perfectly the same. I hope that this post can help someone in the future.

推荐答案

假设 a 在名为 Col1的列中, b 在 Col2中,而 c 在在 Col3中,以下应检查是否存在这样的行:

Assuming a is in a column called "Col1", b is in "Col2" and c is in "Col3", the following should check for the existence of such a row:

self.cur.execute('SELECT * FROM ProSolut WHERE (Col1=? AND Col2=? AND Col3=?)', ('a', 'b', 'c'))
entry = self.cur.fetchone()

if entry is None:
    print 'No entry found'
else:
    print 'Entry found'

这将选择 ProSolut 中与这些值匹配的所有条目。 fetchone 然后尝试获取该查询的结果-如果没有这样的匹配项,则返回 None

This selects all entries in ProSolut that match these values. fetchone then tries to grab a result of this query - if there are no such matches then it returns None.

编辑:与Barmar的评论一致,要插入值,请适应以下内容:

In line with Barmar's comment, to make this insert the values, adapt to the following:

self.cur.execute('SELECT * FROM ProSolut WHERE (Col1=? AND Col2=? AND Col3=?)', ('a', 'b', 'c'))
entry = self.cur.fetchone()

if entry is None:
    self.cur.execute('INSERT INTO ProSolut (Col1, Col2, Col3) VALUES (?,?,?)', ('a', 'b', 'c'))
    print 'New entry added'
else:
    print 'Entry found'

您需要确保您 commit()您的更改也是如此!

You'll need to make sure you commit() your changes too!

这篇关于sqlite3如果不存在则插入(使用Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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