如何正确插入整数sqlite3 python

时间:2017-07-02 20:01:42

标签: python-3.x sqlite

我需要将excel文件中的行插入到我创建的sqlite3数据库中; 到目前为止,我管理我将excel转换为数据帧我创建数据库,我想要的表与字段,我使用for循环通过“插入 tablename 值获取表中的行(?..,?)“,(value1,... valuen)但是只有得到文本类型的日期在数据库中清晰可见,所有整数都以字节的形式传递到数据库中,甚至是int.from_bytes ()不要在正确的形式下得到我的整数.. 所以,如果有人可以提供帮助

devices = df['id_device']
time = df['utc_datetime']
vote_yes = df['yes']
vote_neutre = df['neutre']
vote_no = df['no']

questions = ['question']*len(df)
kpi = ['KPI']*len(df)
id_status = [None]*len(df)
indexing = [index  for index in range(len(df))]
base = list(map(lambda l,t,x,y,z,k,status , quest , index : [l,t.to_datetime(),x,y,z , k , status , quest , index] , devices , time , vote_yes , vote_neutre , vote_no , kpi , id_status , questions , indexing ))

base = [[507, datetime.datetime(2016, 8, 1, 11, 10, 30), 1, 0, 0, 'KPI', None, 'question', 0],
[507, datetime.datetime(2016, 8, 1, 11, 40, 33), 2, 0, 0, 'KPI', None, 'question', 1], 
[507, datetime.datetime(2016, 8, 1, 12, 10, 39), 5, 3, 1, 'KPI', None, 'question', 2], 
[507, datetime.datetime(2016, 8, 1, 13, 10, 43), 1, 0, 0, 'KPI', None, 'question', 3], 
[507, datetime.datetime(2016, 8, 1, 14, 40, 43), 2, 1, 0, 'KPI', None, 'question', 4], 
[507, datetime.datetime(2016, 8, 1, 15, 10, 47), 2, 0, 0, 'KPI', None, 'question', 5], 
 [507, datetime.datetime(2016, 8, 1, 16, 10, 47), 2, 0, 0, 'KPI', None, 'question', 6],
[507, datetime.datetime(2016, 8, 1, 16, 40, 51), 2, 1, 0, 'KPI', None, 'question', 7], 
[507, datetime.datetime(2016, 8, 1, 17, 10, 56), 1, 2, 0, 'KPI', None, 'question', 8], 
[507, datetime.datetime(2016, 8, 1, 17, 40, 57), 1, 0, 0, 'KPI', None, 'question', 9]]


cur = conn.cursor()


cur.execute('''create table if not exists coord4 (device int , time text)''')

for line in base:

    cur.execute('''insert into coord4 values (?,?)''', (line[0], line[1]))
conn.commit()


res = cur.execute('select * from coord4')
print(res.fetchone())
 #output
(b'\xfb\x01\x00\x00\x00\x00\x00\x00', '2016-08-01 11:10:30')

如果你需要,这是我的代码..

1 个答案:

答案 0 :(得分:1)

我正在寻找的解决方案是:

for line in base:

    cur.execute('''insert into coord4 values (?,?)''', (int(line[0]), line[1]))

conn.commit()