我放弃让"sqlite3"工作,但我发现(有帮助("模块"))我有"sqlite"模块.我测试了它(创建表,插入一些值等),它工作正常.但在我开始使用这个模块之前,我想知道它与sqlite3模块相比是否有一些显着的局限性?任何人,请求,请给我建议吗?
先感谢您.
我使用connect()和cursor()来使用SQLite
self.connector = sqlite3.connect(self.dbFile) self.cursor = self.connector.cursor()
并且close()停止使用它.
self.cursor.close()
它们的价格(处理时间)有多贵?它是如此昂贵,只有绝对必要的使用它是必要的吗?或者,在功能中多次使用它是否可以?
我使用以下简单代码进行了测试.proc1()使用在运行查询时始终打开和关闭的代码,proc2()只运行一次.
from sqlite import *
import timeit
import math
def proc1():
db = SQLiteDB("./example.db", False)
db.getOpenRunClose("SELECT * from Benchmark")
db.getOpenRunClose("SELECT * from Benchmark")
db.getOpenRunClose("SELECT * from Benchmark")
db.getOpenRunClose("SELECT * from Benchmark")
db.getOpenRunClose("SELECT * from Benchmark")
db.getOpenRunClose("SELECT * from Benchmark")
def proc2():
db = SQLiteDB("./example.db")
res = db.runSQLToGetResult("SELECT * from Benchmark")
res = db.runSQLToGetResult("SELECT * from Benchmark")
res = db.runSQLToGetResult("SELECT * from Benchmark")
res = db.runSQLToGetResult("SELECT * from Benchmark")
res …Run Code Online (Sandbox Code Playgroud) 我正在使用超过2900万个元素,因此认为数据库比数组更有意义.
以前我只是将一个元素一次传递给execute函数,但我相信一次将100,000个元素的数组传递给executemany函数会更有效.
我将我的180多行代码缩短为这个简短的测试用例:
import sqlite3
if __name__ == '__main__':
connection = sqlite3.connect('array.db')
cursor = connection.cursor()
cursor.execute("create table array (word text);")
cursor.executemany("insert into array values (?)", [u'usa', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'los', u'angeles'])
connection.commit()
cursor.execute("select * from array;")
print cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)
输出:
Traceback (most recent call last):
cursor.executemany("insert into array values (?)", [u'usa', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'sharp', u'rise', u'seen', u'in', u'cd', u'bootlegs', u'los', u'angeles'])
sqlite3.ProgrammingError: Incorrect number …Run Code Online (Sandbox Code Playgroud) 我在我的代码中做这样的事情:
import sqlite3
...
sqlString=company['name']+","+simplejson.dumps(info)
cur.execute("INSERT INTO companyInfo VALUES("+sqlString+")")
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误: cur.execute("INSERT INTO companyBlobs VALUES("+valueString+")") sqlite3.OperationalError: unrecognized token: "{"
我想这是转义 JSON 数据的问题,但不确定如何解决。想法?
我正在使用pysqlite与SQLite数据库进行对话,我想知道检查UPDATESQL语句是否实际上已成功更新表中的内容的正确方法是什么.
是否有一个变量,我可以在执行后快速检查pysqlite?
在Android上,我非常习惯于使用cursor.moveToLast()来获取游标中的最后一项。但是,我似乎找不到与Python的SQLite相当的产品。有没有可以让我获得游标最后一行的功能?
我可以只调用cursor.fetchall()并获取列表中的最后一项,但是还有什么比这更有效的吗?谢谢。
我认为我是一个傻瓜,也许不是进口正确的包装,但是当我做...
from pysqlite2 import dbapi2 as sqlite
import types
import re
import sys
...
def create_asgn(self):
stmt = "CREATE TABLE ? (login CHAR(8) PRIMARY KEY NOT NULL, grade INTEGER NOT NULL)"
stmt2 = "insert into asgn values ('?', ?)"
self.cursor.execute(stmt, (sys.argv[2],))
self.cursor.execute(stmt2, [sys.argv[2], sys.argv[3]])
...
I get the error pysqlite2.dbapi2.OperationalError: near "?": syntax error
这对我来说没什么意义,因为文档显示pysqlite是qmark参数化的.我是python和db-api的新手,帮助我!谢谢
我有两个单独的数据库文件,每个文件的表都具有匹配的主键,但数据不同。我想根据另一个表中的值从一个表中提取行。在 CLI for 中sqlite3,我会这样做:
.open data.db
.open details.db
attach 'data.db' as data;
attach 'details.db' as details;
select details.A.colA from data.A join details.A using ('key') where data.A.colB = 0 and data.A.colC = 1;
Run Code Online (Sandbox Code Playgroud)
如何使用 重新创建这样的跨数据库连接pysqlite?
情况:需要在 SQLite 数据库中插入相当多的数据。
问题:我们可以使用两条语句来插入数据 -
data = [("111", "222", "333"), ("AAA", "BBB", "CCC"), ("XXX", "YYY", "ZZZ")]
#method1
for item in data:
cursor.execute("INSERT INTO table(value1, value2, value3) VALUES (?,?,?)", item)
conn.commit()
#method2
cursor.execute("INSERT INTO table(value1, value2, value3) VALUES(?,?,?)", data)
conn.commit()
Run Code Online (Sandbox Code Playgroud)
问题:如果忽略速度,从编程的角度来看,哪种做法更好?如果可能,还要解释原因。