我想将数据帧解析为sql表中的两个预定义列。 sql中的模式为:
abc(varchar(255))
def(varchar(255))
具有这样的数据框:
df = pd.DataFrame(
[
[False, False],
[True, True],
],
columns=["ABC", "DEF"],
)
并且sql查询是这样的:
with conn.cursor() as cursor:
string = "INSERT INTO {0}.{1}(abc, def) VALUES (?,?)".format(db, table)
cursor.execute(string, (df["ABC"]), (df["DEF"]))
cursor.commit()
以便查询(字符串)看起来像这样:
'INSERT INTO my_table(abc, def) VALUES (?,?)'
这将创建以下错误消息:
pyodbc.Error: ('HY004', '[HY004] [Cloudera][ODBC] (11320) SQL type not supported. (11320) (SQLBindParameter)')
因此,我尝试在以下Impala编辑器中使用直接查询(不通过Python):
'INSERT INTO my_table(abc, def) VALUES ('Hey','Hi');'
并产生此错误消息:
AnalysisException: Possible loss of precision for target table 'my_table'. Expression ''hey'' (type: `STRING) would need to be cast to VARCHAR(255) for column 'abc'`
为什么我什至不能在表中插入简单的字符串,例如“嗨”?我的架构设置正确还是其他?
STRING
类型的大小限制为2GB。VARCHAR
的长度是您定义的长度,但不能超过64KB。因此,如果您将一个隐式转换为另一个,则可能会丢失数据。
默认情况下,文字被视为类型STRING
。因此,为了将文字插入VARCHAR
字段,您需要适当地将其CAST
插入。
INSERT INTO my_table(abc, def) VALUES (CAST('Hey' AS VARCHAR(255)),CAST('Hi' AS VARCHAR(255)));