试图导出一个SQL表,但使用Python+ pyodbc和pandas

发布于 2025-02-08 07:41:39 字数 786 浏览 0 评论 0原文

您好,我试图将非常大的SQL表(约170万行)导出到Excel或CSV文件中,我的问题是当我不指定块质量时,我会遇到内存限制错误,我猜数据太大了存储在内存中。现在,我已经通过了一个大块,我遇到了不同的错误。

错误 - VisibleDeprecationWarning:从破烂的嵌套序列中创建一个ndarray(这是列出的或列表,或tuples-or-tuples-or-tuples-or ndarrays或不同长度或形状的ndarray)。如果要执行此操作,则必须在创建NDARRAY时指定“ dtype =对象”。 值= np.Array([v in values中的V convert(v)])

代码:

import pyodbc
import pandas as pd


conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=servername;'
                      'Database=DB01;'
                      'Trusted_Connection=yes;')


sql_query = pd.read_sql_query("select * from calltype", conn, chunksize=1000, dtype=object)
df = pd.DataFrame(sql_query)
df.to_excel(r'C:\Users\user\Documents\BackupTest1.xlsx', index=False)
print("Records Exported")'''

Hello I am trying to export a very large SQL table (approx 1.7million rows) into either an excel or csv file, my problem is when I dont specify a chunksize I am getting a memory limit error, I am guessing the data is too large to store in memory. Now that I have passed in a chunksize I am getting a different error.

Error - VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
values = np.array([convert(v) for v in values])

Code:

import pyodbc
import pandas as pd


conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=servername;'
                      'Database=DB01;'
                      'Trusted_Connection=yes;')


sql_query = pd.read_sql_query("select * from calltype", conn, chunksize=1000, dtype=object)
df = pd.DataFrame(sql_query)
df.to_excel(r'C:\Users\user\Documents\BackupTest1.xlsx', index=False)
print("Records Exported")'''

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

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

发布评论

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

评论(1

一页 2025-02-15 07:41:39

我会查看本文,看看这些解决方案中的任何一个是否适合您。

绕过pandas记忆限制

基于您写的内容的错误,因此,如果您的数据正确导出,这可能不是问题,但是您进行了一些数据验证以检查。另外,将dtype定义为对象可能会使警告消失。

I would check out this article and see if any of these solutions work for you.

Bypassing Pandas Memory Limitations

The message you are getting is just a warning not an error based on what you wrote so if your data exports correctly this may not be an issue but you do some data validation to check. Alternatively, defining the dtype as an object may make the warning go away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文