我创建了凤凰表:
create table movement_record(
id varchar not null,
latitude float,
longitude float,
location varchar,
duration float,
start_time varchar not null,
end_time varchar not null,
CONSTRAINT pk PRIMARY KEY (id, start_time, end_time)
);
我想在Phoenix表上面写一个列表列表。列表(final_output
)数据变量列表为[[string,float,float,string,float,datetime,datetime],[---],--]
我想使用以下代码在Phoenix中写入数据:
_phoenixdb = phoenixdb
conn = _phoenixdb.connect(connection_string, autocommit=True)
cursor = conn.cursor()
for row in final_output:
id=row[0]
latitude=row[1]
longitude=row[2]
ignition_on=row[3]
location=row[4]
duration=row[5]
start_time=row[6]
end_time=row[7]
sql = "UPSERT INTO movement_record VALUES (?,?,?,?,?,?,?,?)", (truck_id,latitude,longitude,ignition_on,location,duration,start_time,end_time)
cursor.execute(sql)
我收到错误:
Traceback (most recent call last):
File "main.py", line 61, in output_to_phoenix
cursor.execute(sql)
File "/usr/local/lib/python2.7/dist-packages/phoenixdb/cursor.py", line 201, in execute
operation, first_frame_max_size=self.itersize)
File "/usr/local/lib/python2.7/dist-packages/phoenixdb/avatica.py", line 399, in prepare_and_execute
request.sql = sql
TypeError: ('UPSERT INTO movement_record VALUES (?,?,?,?,?,?,?,?)', (u'SRT1', -23.333999633789062, has type tuple, but expected one of: bytes, unicode
这看起来很明显,但我无法弄清楚我在这里缺少什么。帮助将不胜感激。感谢。
答案 0 :(得分:2)
嗯,第一个参数cursor.execute
是一个字符串,第二个参数是元组。我建议改变这样的代码:
sql = "UPSERT INTO movement_record VALUES (?,?,?,?,?,?,?,?)"
params = (truck_id,latitude,longitude,ignition_on,location,duration,start_time,end_time)
cursor.execute(sql, params)
如果你想保留预先构造的参数元组,你可以这样做:
cursor.execute(*sql)
这将扩展元组并将项目作为单独的参数传递。