简体   繁体   English

带有Python的Python异常No描述read_sql

[英]Python Exception No Description with Pandas read_sql

Using Python, I'm trying to read a table from SQL Server and then insert the data into a table in Access. 我正在尝试使用Python从SQL Server中读取表,然后将数据插入Access中的表中。 The best way I've found to do this is using the pandas dataframe. 我发现做到这一点的最佳方法是使用pandas数据框。 I wrote up a program that reads a SQL Server table into a dataframe like so: 我编写了一个程序,将SQL Server表读入数据帧,如下所示:

dataframe = pandas.read_sql(selectSql, srcConn)

And it works great on a ~209MB table. 它在约209MB的表上运行良好。 When I try it on a ~1,116MB table it throws an exception with no description. 当我在〜1,116MB的表上尝试时,会抛出一个没有描述的异常。 I'm guessing it has to do with the size of the table it's reading in (it would be nice if it said that). 我猜想这与它正在读取的表的大小有关(如果这样的话,那会很好)。 I know Access can only hold 2GB but there is plenty of room left in it and it doesn't even get to the part where it writes to Access before throwing the error. 我知道Access只能容纳2GB,但其中还有很多空间,在抛出错误之前,它甚至没有到达写入Access的部分。

Is there any way to fix this for larger tables? 有什么办法可以解决较大的表吗? Is there a better way I should be copying tables from SQL Server 2008 R2 to Access 2016 using Python? 是否有更好的方法使用Python将表从SQL Server 2008 R2复制到Access 2016? I have 16GB of RAM on Win10 64-bit so that shouldn't be a problem. 我在Win10 64位上有16GB的RAM,所以这应该不是问题。 I've tried 32-bit Python 3.7 and 64-bit Python 3.6 to no avail. 我尝试了32位Python 3.7和64位Python 3.6都没有用。 I tried SSIS first but it crashes my entire Visual Studio whenever I try to open a package with a connection to Access. 我先尝试了SSIS,但是每当尝试打开与Access的连接的程序包时,它都会崩溃整个Visual Studio。

UPDATE: 更新:

I followed Gord's advice below and now my code looks like this: 我遵循以下Gord的建议,现在我的代码如下所示:

access_cnxn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=' + access_db + ';'
)
sqls_cnxn_str = (
    r'DRIVER=ODBC Driver 13 for SQL Server;'
    r'SERVER=' + sqls_server + ';'
    r'DATABASE=' + sqls_db + ';'
    r'UID=' + sqls_username + ';'
    r'PWD=' + sqls_password + ';'
)

This connection works by itself: 此连接本身起作用:

sqls_cnxn = pyodbc.connect(sqls_cnxn_str)

And this connection works by itself: 这种连接本身是有效的:

pyodbc.pooling = False
access_cnxn = pyodbc.connect(access_cnxn_str, autocommit = True)

But this is throwing an error: 但这会引发错误:

access_cnxn.execute(f"SELECT * INTO {access_table} FROM [ODBC;{sqls_cnxn_str}].{sqls_table}")

The error thrown: 引发的错误:

Message=('HY000', "[HY000] [Microsoft][ODBC Microsoft Access Driver] ODBC--connection to 'ODBC Driver 13 for SQL ServerSERVERNAME' failed. (-2001) (SQLExecDirectW)") 消息=(“ HY000”,“ [HY000] [Microsoft] [ODBC Microsoft Access驱动程序] ODBC--连接到“用于SQL ServerSERVERNAME的ODBC驱动程序13”失败(-2001)(SQLExecDirectW)”)
Source=C:\\Users\\bruescm\\source\\repos\\DB_Test\\DB_Test\\SyncAllTests.py 源= C:\\用户\\ bruescm \\源\\回购\\ DB_Test \\ DB_Test \\ SyncAllTests.py
StackTrace: File "C:\\Users\\bruescm\\source\\repos\\DB_Test\\DB_Test\\SyncAllTests.py", line 57, in sync_table dest_cnxn.execute(f"SELECT * INTO {access_table} FROM [ODBC;{sqls_cnxn_str}].{sqls_table}") File "C:\\Users\\bruescm\\source\\repos\\DB_Test\\DB_Test\\SyncAllTests.py", line 121, in main sync_table('', sqls_table, get_access_cnxn(), access_table) File "C:\\Users\\bruescm\\source\\repos\\DB_Test\\DB_Test\\SyncAllTests.py", line 124, in main() StackTrace:文件“ C:\\ Users \\ bruescm \\ source \\ repos \\ DB_Test \\ DB_Test \\ SyncAllTests.py”,第57行,位于sync_table dest_cnxn.execute(f“ SELECT * INTO {access_table} FROM [ODBC; {sqls_cnxn_str}]]中。 {sqls_table}“)文件“ C:\\ Users \\ bruescm \\ source \\ repos \\ DB_Test \\ DB_Test \\ SyncAllTests.py”,第121行,位于主sync_table('',sqls_table,get_access_cnxn(),access_table)文件“ C:\\ Users \\ bruescm \\ source \\ repos \\ DB_Test \\ DB_Test \\ SyncAllTests.py“,第124行,位于main()中

SERVERNAME in the error is the name of the server on which SQL Server resides. 错误中的SERVERNAME是SQL Server所在的服务器的名称。 Not sure why it jammed it up against the driver name in the error. 不知道为什么将其与错误中的驱动程序名称相对应。

Any ideas? 有任何想法吗?

UPDATE 2: 更新2:

It turns out my Access is 32-bit. 原来我的访问权限是32位的。 This still doesn't explain why it won't connect as I was originally using Python 3.7 32-bit. 这仍然不能解释为什么它无法连接,因为我最初使用的是Python 3.7 32位。

Thanks. 谢谢。

I was able to get the Access Database Engine to pull a table from SQL Server and create a copy in the Access database by simply doing 我能够通过简单的操作使Access数据库引擎从SQL Server中提取表并在Access数据库中创建一个副本

pyodbc.pooling = False  # required
cnxn = pyodbc.connect("DSN=myAccessDb", autocommit=True)
cnxn.execute("SELECT * INTO access_tbl FROM [ODBC;DSN=SQLmyDb].sql_server_tbl")

where SQLmyDb is the ODBC DSN for my SQL Server instance. 其中, SQLmyDb是我的SQL Server实例的ODBC DSN。

Update 更新

Just tested to confirm that DSN-less connection strings also work: 刚刚进行测试以确认无DSN的连接字符串也可以正常工作:

pyodbc.pooling = False  # required
access_cnxn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=C:\__tmp\test.accdb;'
)
cnxn = pyodbc.connect(access_cnxn_str, autocommit=True)
sql_cnxn_str = (
    r'DRIVER=ODBC Driver 17 for SQL Server;'
    r'SERVER=(local)\SQLEXPRESS;'
    r'DATABASE=myDb;'
    r'Trusted_Connection=Yes;'
)
cnxn.execute(f"SELECT * INTO access_tbl FROM [ODBC;{sql_cnxn_str}].sql_server_tbl")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM