【问题标题】:Error while retrieving data from SQL Server using pyodbc python使用 pyodbc python 从 SQL Server 检索数据时出错
【发布时间】:2019-07-16 22:55:07
【问题描述】:

我的表格数据有 5 列和 5288 行。我正在尝试将该数据读入添加列名的 CSV 文件中。代码如下所示:

cursor = conn.cursor()
cursor.execute('Select * FROM classic.dbo.sample3')

rows = cursor.fetchall()

print ("The data has been fetched")

dataframe = pd.DataFrame(rows, columns =['s_name', 't_tid','b_id', 'name', 'summary'])
dataframe.to_csv('data.csv', index = None)

数据是这样的

s_sname   t_tid   b_id  name   summary
---------------------------------------------------------------------------
db1       001     100   careie  hello this is john speaking blah blah blah

它看起来像上面,但有 5288 个这样的行。

当我尝试执行上面提到的代码时,它会抛出一个错误:

ValueError: 传递值的形状是 (5288, 1),索引意味着 (5288, 5)

我不明白我做错了什么。

【问题讨论】:

    标签: sql-server python-3.x pyodbc


    【解决方案1】:

    使用这个。

    dataframe = pd.read_sql('Select * FROM classic.dbo.sample3',con=conn)
    dataframe.to_csv('data.csv', index = None)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2021-09-12
      • 2014-08-09
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多