在凤凰中插入多列:python

insert multiple columns in phoenix: python

我创建了凤凰 table 喜欢:

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 table。列表列表(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

这看起来是显而易见的解决方案,但我无法弄清楚我在这里遗漏了什么。帮助将不胜感激。谢谢。

嗯,第一个参数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)

这将扩展元组并将项目作为单独的参数传递。