如何使用pandas.read_sql,但要保持划分格式?

发布于 2025-01-28 16:28:07 字数 1678 浏览 4 评论 0原文

自早晨以来,我一直在挣扎。我有一个带有Rowversion列的SQL表,我正在使用以下代码来创建:

mycursor = cnxn.cursor()
        
s = """
CREATE TABLE table_name (
  PersonID int PRIMARY KEY,
  Name varchar(255),
  RowVersion rowversion);
  """
        
mycursor.execute(s)
        
sql = "INSERT INTO table_name (PersonID, Name) VALUES (?, ?)"
val = [
       (1, 'Andres'),
       (2, 'Carl'),
       (3, 'Tracy'),
       ]
        
        
mycursor.executemany(sql, val)
cnxn.commit()

输出表:

personidNameRowversion
1AndresAaaaaaaaad70 =
2CarlAAAAAAAAAAD74 =
3TracyAAAAAAAAAAAAD78 =

但是一旦我读取了我阅读的表使用pd.pd.read_sqlpd.read_sql_query列更改为以下:

dfTable = pd.read_sql("SELECT * FROM table_name", cnxn, coerce_float=False)
dfTable.head()
personIdnamerowversion
1andresb'\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ \ \ \ \ \ \ \ \ \ x0f \ xba'2
b'\ x00carlb'\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \
x00 \ x0f \ xbb'3tracy\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x0f \ xbc \ xbc

'喜欢获得的是像以前一样保留Rowversion格式。根据

有能力找到解决方案吗?

谢谢你,

I've been struggling since the morning with this. I have a SQL table with a RowVersion column, that I'm creating using the following code:

mycursor = cnxn.cursor()
        
s = """
CREATE TABLE table_name (
  PersonID int PRIMARY KEY,
  Name varchar(255),
  RowVersion rowversion);
  """
        
mycursor.execute(s)
        
sql = "INSERT INTO table_name (PersonID, Name) VALUES (?, ?)"
val = [
       (1, 'Andres'),
       (2, 'Carl'),
       (3, 'Tracy'),
       ]
        
        
mycursor.executemany(sql, val)
cnxn.commit()

Output table:

PersonIDNameRowVersion
1AndresAAAAAAAAD70=
2CarlAAAAAAAAD74=
3TracyAAAAAAAAD78=

But once I read the table using pd.read_sql or pd.read_sql_query the column changes to the following:

dfTable = pd.read_sql("SELECT * FROM table_name", cnxn, coerce_float=False)
dfTable.head()
PersonIDNameRowVersion
1Andresb'\x00\x00\x00\x00\x00\x00\x0f\xba'
2Carlb'\x00\x00\x00\x00\x00\x00\x0f\xbb'
3Tracyb'\x00\x00\x00\x00\x00\x00\x0f\xbc''

What I would like to get is to keep the RowVersion format as before. According to the pd.read_sql documentation there is a way to avoid converting non-string values to floating points, but since it is a hex value pandas is reading as a string.

Is there a workaround to find a solution?

Thank you,

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

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

发布评论

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

评论(1

雨夜星沙 2025-02-04 16:28:07

在将数据框架上传为表之后,我最终再次阅读并更改了表...

s = """
ALTER TABLE [dbo].[table_name] ADD rv rowversion NULL
"""

mycursor.execute(s)
cnxn.commit()

请随时回答,如果有另一个解决方案,Ty。

I ended up reading and altering the table again after uploading the Dataframe as table...

s = """
ALTER TABLE [dbo].[table_name] ADD rv rowversion NULL
"""

mycursor.execute(s)
cnxn.commit()

Please, feel free to answer if there is another solution, ty.

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