使用boto3将枚举类型插入aurora postgres
inserting enum types to aurora postgres using boto3
我有一个模式,它有一个字符串和枚举,我正尝试使用 boto3 写入。我创建一个sql,指定参数和运行 execute_statement。字符串和日期足够简单:
sql = """
insert into scrape_results(lname,fname, dob) values (:lname, :fname, :dob)
"""
param1 = {'name':'lname', 'value':{'stringValue': 'xx'}}
param3 = {'name':'dob', 'typeHint': 'DATE', 'value':{'stringValue': '1970-01-01'}}
param_set = [param1, param2]
response = rds_data.execute_statement(
resourceArn = cluster_arn,
secretArn = secret_arn,
database = 'db',
sql = sql,
parameters = param_set)
我的第三个字段是一个枚举。我不遵循如何插入枚举。枚举没有类型提示,boto 不接受字符串值。我试过这个:(court_type 是枚举)
param3 = {'name':'court', 'value':{'stringValue': 'court'}}
An error occurred (BadRequestException) when calling the ExecuteStatement operation:
ERROR: operator does not exist: court_type = character varying
Hint: No operator matches the given name and argument type(s).
You might need to add explicit type casts
我用这个来绕过它:
sql_statement = f"""
INSERT INTO
s_info(jurisdiction, court, us_state)
VALUES(
'{req.jurisdiction}',
'{req.court_type}', #enum
'{req.state}'
)
ON CONFLICT DO NOTHING
"""
rds_data = boto3.client('rds-data', region_name='xxx')
try:
response = rds_data.execute_statement(
resourceArn=CLUSTER_ARN,
secretArn=SECRET_ARN,
database=DATABASE,
sql=sql_statement
)
except ClientError as exc:
#print(f"failed to execute {sql_statement} because {exc}")
raise
鉴于您的枚举名为 court_type,您可以使用这样的字符串参数在语句中设置值 ... values(:jurisdiction, cast(:court_type as court_type), :state) ...
。 cast 会将来自 aws 数据 api 的字符串转换为 court_type 类型。
我有一个模式,它有一个字符串和枚举,我正尝试使用 boto3 写入。我创建一个sql,指定参数和运行 execute_statement。字符串和日期足够简单:
sql = """
insert into scrape_results(lname,fname, dob) values (:lname, :fname, :dob)
"""
param1 = {'name':'lname', 'value':{'stringValue': 'xx'}}
param3 = {'name':'dob', 'typeHint': 'DATE', 'value':{'stringValue': '1970-01-01'}}
param_set = [param1, param2]
response = rds_data.execute_statement(
resourceArn = cluster_arn,
secretArn = secret_arn,
database = 'db',
sql = sql,
parameters = param_set)
我的第三个字段是一个枚举。我不遵循如何插入枚举。枚举没有类型提示,boto 不接受字符串值。我试过这个:(court_type 是枚举)
param3 = {'name':'court', 'value':{'stringValue': 'court'}}
An error occurred (BadRequestException) when calling the ExecuteStatement operation:
ERROR: operator does not exist: court_type = character varying
Hint: No operator matches the given name and argument type(s).
You might need to add explicit type casts
我用这个来绕过它:
sql_statement = f"""
INSERT INTO
s_info(jurisdiction, court, us_state)
VALUES(
'{req.jurisdiction}',
'{req.court_type}', #enum
'{req.state}'
)
ON CONFLICT DO NOTHING
"""
rds_data = boto3.client('rds-data', region_name='xxx')
try:
response = rds_data.execute_statement(
resourceArn=CLUSTER_ARN,
secretArn=SECRET_ARN,
database=DATABASE,
sql=sql_statement
)
except ClientError as exc:
#print(f"failed to execute {sql_statement} because {exc}")
raise
鉴于您的枚举名为 court_type,您可以使用这样的字符串参数在语句中设置值 ... values(:jurisdiction, cast(:court_type as court_type), :state) ...
。 cast 会将来自 aws 数据 api 的字符串转换为 court_type 类型。