如何使用pandas.read_sql,但要保持划分格式?
自早晨以来,我一直在挣扎。我有一个带有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()
输出表:
personid | Name | Rowversion |
---|---|---|
1 | Andres | Aaaaaaaaad70 = |
2 | Carl | AAAAAAAAAAD74 = |
3 | Tracy | AAAAAAAAAAAAD78 = |
但是一旦我读取了我阅读的表使用pd.pd.read_sql
或pd.read_sql_query
列更改为以下:
dfTable = pd.read_sql("SELECT * FROM table_name", cnxn, coerce_float=False)
dfTable.head()
personId | name | rowversion |
---|---|---|
1 | andres | b'\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ \ \ \ \ \ \ \ \ \ x0f \ xba'2 |
b'\ x00 | carl | b'\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ |
x00 \ x0f \ xbb'3 | tracy | \ 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:
PersonID | Name | RowVersion |
---|---|---|
1 | Andres | AAAAAAAAD70= |
2 | Carl | AAAAAAAAD74= |
3 | Tracy | AAAAAAAAD78= |
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()
PersonID | Name | RowVersion |
---|---|---|
1 | Andres | b'\x00\x00\x00\x00\x00\x00\x0f\xba' |
2 | Carl | b'\x00\x00\x00\x00\x00\x00\x0f\xbb' |
3 | Tracy | b'\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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在将数据框架上传为表之后,我最终再次阅读并更改了表...
请随时回答,如果有另一个解决方案,Ty。
I ended up reading and altering the table again after uploading the Dataframe as table...
Please, feel free to answer if there is another solution, ty.