使用pyodbc连接sqlite3 for fast_executemany

发布于 2025-01-27 09:58:59 字数 1270 浏览 2 评论 0原文

目前,我们使用的程序可以创建并将大型数据集写入数据库,但是该过程可能需要很长时间。我们正在尝试将CURSOR.FAST_EXECUTEMANY = TRUE从SQLalchemy组合在一起,以改善这些数据库的写入时间。当我尝试使用sqlite3和pyodbc创建引擎时,我的代码错误在此处:

import pandas as pd
from sqlite3 import connect
import pyodbc
from sqlalchemy import create_engine

engine = create_engine('SQLite3 ODBC Driver+pyodbc:///C:\\Users\\Documents\\PythonScripts\\FLR.sosat')

conn = engine.connect()
c = conn.cursor()

我们尝试了多种方法,以指定驱动程序和服务器以及类似的内容:

# conn = pyodbc.connect('DRIVER={SQL Server};'
#                       'SERVER=localhost;'
#                       'DATABASE=C:\\Users\\Documents\\PythonScripts\\FLR.sosat')

单个引擎线似乎是由于最接近的工作我们从上面注释的代码中接收驱动程序和服务器错误。我们已经从

我们收到参数:无法从字符串中解析RFC1738 URL。

我们将感谢有关如何将SQLITE3数据库转到PyoDBC以及如何提高写入速度的任何帮助或想法。谢谢!

Note .sosat文件是一个使用SQLite3的数据库文件,它应该像任何.db文件一样工作,

我们从这里尝试了修复程序:使用pyodbc,python 连接到sqlite3服务器,这对我们不起作用,我们收到了驾驶员错误: InterfaceError :('im002','[im002] [Microsoft] [ODBC驱动程序管理器]找不到数据源名称,并且未指定默认驱动程序(0)(sqldriverConnect)')

We currently use a program that creates and writes large datasets to databases, but the process can take a long time. We are trying to incorporate cursor.fast_executemany = True from sqlalchemy to improve the write times to these databases. My code errors out when I try to create an engine using SQLite3 and pyodbc here:

import pandas as pd
from sqlite3 import connect
import pyodbc
from sqlalchemy import create_engine

engine = create_engine('SQLite3 ODBC Driver+pyodbc:///C:\\Users\\Documents\\PythonScripts\\FLR.sosat')

conn = engine.connect()
c = conn.cursor()

We have tried numerous ways where we specify the driver and server and things like that like the following:

# conn = pyodbc.connect('DRIVER={SQL Server};'
#                       'SERVER=localhost;'
#                       'DATABASE=C:\\Users\\Documents\\PythonScripts\\FLR.sosat')

The single engine line seems to be the closest to working due to us receiving driver and server errors from the commented out code above. We have downloaded the ODBC driver from http://www.ch-werner.de/sqliteodbc/

We receive the ArgumentError: Could not parse rfc1738 URL from string.

We would appreciate any help or ideas on how to get the SQLite3 database to pyodbc and how to improve the write speed. Thanks!

Note the .sosat file is a database file that uses sqlite3, it should work like any .db file

We tried the fix from here: Connect to SQLite3 server using PyODBC, Python and that did not work for us, we received the driver error:
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2025-02-03 09:58:59

您要完成的工作不会有两个原因:

原因1:

Sqlalchemy不支持PyoDBC作为 sqlite 的dbapi层。

原因2:

即使SQLalchemy确实支持SQLITE+PYODBC:// SQLITE ODBC驱动程序必须支持“参数数组”,这是一个可选的ODBC功能,该功能fast_exececutemany = true true use做魔术。并非所有的ODBC驱动程序支持fast_executemany = true如图所示在这里。使用Vanilla PyoDBC进行快速测试表明,“ SQLITE3 ODBC驱动程序”不支持它,实际上它崩溃了Python Instrymer :(

crsr.fast_executemany = True
crsr.executemany(
    f"INSERT INTO {table_name} (txt) VALUES (?)", [("foo",), ("bar",)]
)
# Process finished with exit code -1073741819 (0xC0000005)

错误0xc0000005是“访问违规”。)

What you are trying to accomplish will not work for two reasons:

Reason 1:

SQLAlchemy does not support pyodbc as a DBAPI layer for SQLite.

Reason 2:

Even if SQLAlchemy did support sqlite+pyodbc:// the SQLite ODBC Driver would have to support "parameter arrays", an optional ODBC feature that fast_executemany = True uses to do its magic. Not all ODBC drivers support fast_executemany = True as shown here. A quick test with vanilla pyodbc shows that "SQLite3 ODBC Driver" doesn't support it, in fact it crashes the Python interpreter:

crsr.fast_executemany = True
crsr.executemany(
    f"INSERT INTO {table_name} (txt) VALUES (?)", [("foo",), ("bar",)]
)
# Process finished with exit code -1073741819 (0xC0000005)

(Error 0xC0000005 is "Access Violation".)

橘味果▽酱 2025-02-03 09:58:59

你尝试过吗?

import sqlite3
db = r'C:\Users\Documents\PythonScripts\FLR.sosat'
conn = sqlite3.connect(db)
print('connection established')

Have you tried

import sqlite3
db = r'C:\Users\Documents\PythonScripts\FLR.sosat'
conn = sqlite3.connect(db)
print('connection established')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文