我正在尝试这段代码:
import sqlite
connection = sqlite.connect('cache.db')
cur = connection.cursor()
cur.execute('''create table item
(id integer primary key, itemno text unique,
scancode text, descr text, price real)''')
connection.commit()
cur.close()
Run Code Online (Sandbox Code Playgroud)
我抓住了这个例外:
Traceback (most recent call last):
File "cache_storage.py", line 7, in <module>
scancode text, descr text, price real)''')
File "/usr/lib/python2.6/dist-packages/sqlite/main.py", line 237, in execute
self.con._begin()
File "/usr/lib/python2.6/dist-packages/sqlite/main.py", line 503, in _begin
self.db.execute("BEGIN")
_sqlite.OperationalError: database is locked
Run Code Online (Sandbox Code Playgroud)
cache.db的权限没问题.有任何想法吗?
我正试图pysqlite从我的系统中删除pip.
我这样做是没有意义的:
$ pip uninstall pysqlite
Run Code Online (Sandbox Code Playgroud)
该命令有效,但请注意:
$ pip freeze
[...]
pysqlite==1.0.1
Run Code Online (Sandbox Code Playgroud)
让我们再试一次
$ pip uninstall pysqlite
Can't uninstall 'pysqlite'. No files were found to uninstall.
Run Code Online (Sandbox Code Playgroud)
诺普,似乎被删除但仍然出现 pip freeze
现在来了很有趣
$ pip install pysqlite
Requirement already satisfied (use --upgrade to upgrade): pysqlite in /usr/lib/python2.6/dist-packages
Cleaning up...
Run Code Online (Sandbox Code Playgroud)
很公平:
$ pip install -U pysqlite
[...]
error: command 'gcc' failed with exit status 1
[...]
Can't roll back pysqlite; was not uninstalled
[...]
Run Code Online (Sandbox Code Playgroud)
我只是不明白.为什么不能pip卸载pysqlite?
在这篇关于SQLite的帖子中,aaronasterling告诉我
cmd = "attach \"%s\" as toMerge" % "b.db" : 是错的cmd = 'attach "{0}" as toMerge'.format("b.db") : 是正确的cmd = "attach ? as toMerge"; cursor.execute(cmd, ('b.db', )) :是对的但是,我认为第一和第二是相同的.这三者有什么不同?
使用pysqlite我正在制作一个程序来处理某些数据.同样种类的操作是在多个表和列类似领域完成的,所以我想我可以参数化的SQL语句,如下所示:
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))
Run Code Online (Sandbox Code Playgroud)
我得到的错误是不是非常有帮助(sqlite3.OperationalError: near "?": syntax error),但我得到了这一点:在以这种方式使用的占位符Pysqlite并不领情.
谁能指出这里发生了什么以及正确的做法?
我试图使用executemany将值插入数据库,但它对我不起作用.这是一个示例:
clist = []
clist.append("abc")
clist.append("def")
clist.append("ghi")
cursor.executemany("INSERT INTO myTable(data) values (?) ", clist)
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
但是,当我更改列表时,它工作正常:
clist = ["a", "b"]
cursor.executemany("INSERT INTO myTable(data) values (?) ", clist)
Run Code Online (Sandbox Code Playgroud)
它按预期工作!我可以在数据库中看到数据.为什么第一个列表不起作用而第二个列表不起作用?
(PS:这只是一个示例,而不是实际代码.为简单起见,我做了一个小测试用例).
我目前在sqlite数据库中持久存在文件名以用于我自己的目的.每当我尝试插入具有特殊字符的文件(如é等)时,它会抛出以下错误:
pysqlite2.dbapi2.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
Run Code Online (Sandbox Code Playgroud)
当我通过使用unicode方法将发送到pysqlite的值包装为"将我的应用程序切换到Unicode字符串"时unicode(filename),它会抛出此错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我有什么办法可以摆脱这个吗?修改我的所有文件以符合不是一个选项.
更新
如果我通过解码文本filename.decode("utf-8"),我仍然得到上面的ProgrammingError.
我的实际代码如下所示:
cursor.execute("select * from musiclibrary where absolutepath = ?;",
[filename.decode("utf-8")])
Run Code Online (Sandbox Code Playgroud)
我的代码应该是什么样的?
嗨,我正在使用Ubuntu版本12.10(量子)32位与Linux内核3.5.0-21通用.我正试图让IPython的历史发挥作用.我已经使用pythonbrew和虚拟环境进行了设置.在那里我使用pip来安装IPython.目前,当我在终端启动IPython时,我得到:
WARNING: IPython History requires SQLite, your history will not be saved
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Run Code Online (Sandbox Code Playgroud)
搜索第一行中的警告,我找到了此问题报告,因此我返回并安装了以下内容:
sudo apt-get install libsqlite0 libsqlite0-dev libsqlite3-0 libsqlite3-dev
Run Code Online (Sandbox Code Playgroud)
然后使用pip删除并重新安装pysqlite
pip uninstall pysqlite
pip install pysqlite
Run Code Online (Sandbox Code Playgroud)
之后我想我会通过导入模块来检查安装: …
在pysqlite中,违反NOT NULL或UNIQUE约束同样会引发IntegrityError.不幸的是,此Exception类型不提供错误代码,而只提供消息.
所以,假设我想忽略唯一约束违规,因为我知道这对给定数据是安全的,但是应该报告关键列中的Null值.
我想出了以下解决方案:
con = sqlite3.connect(':MEMORY:')
con.execute('''CREATE TABLE ABCD (A TEXT NOT NULL,
B TEXT NOT NULL,
C TEXT NOT NULL,
D TEXT NOT NULL,
PRIMARY KEY (A, B))''')
with con:
for a, b, c, d in inputs:
try:
con.execute('INSERT INTO ABCD VALUES (?, ?, ?, ?)',
(a, b, c, d))
except sqlite3.IntegrityError as e:
# Skip 'not unique' errors, but raise others.
if not e.message.endswith('not unique'):
raise
con.close()
Run Code Online (Sandbox Code Playgroud)
但是,解析错误消息似乎是错误的,可能不可靠.有没有更好的方法来做到这一点,甚至可能使用con.executemany()?
我正在尝试安装pysqlite并遇到麻烦.我发现最可能的原因是缺少sqlite头文件,我必须安装它们.
但是,我不知道这些标题是什么(我可以找到它们,它们正在做什么以及如何安装它们).
任何人,请求,帮助我吗?
为什么
from pysqlite2 import dbapi2 as sqlite
Run Code Online (Sandbox Code Playgroud)
原因
ImportError:没有名为pysqlite2的模块
是不是已经在Python 2.6.5中安装了pysqlite2?