尽管提交,Python SQLite插入仍无法正常工作

时间:2018-11-28 10:37:57

标签: python-2.7 sqlite

这是我在Python中运行的代码。该表已经在数据库中创建。我正在执行提交,所以我不知道它为什么起作用。

代码执行得很好,但是没有数据插入表中。我直接通过sqlite命令行运行了相同的insert语句,效果很好。

import os
import sqlite3

current_dir = os.path.dirname(__file__)
db_file = os.path.join(current_dir, '../data/trips.db')
trips_db = sqlite3.connect(db_file)
c = trips_db.cursor()
print 'inserting data into aggregate tables'
c.execute(
    '''
    insert into route_agg_data
        select
        pickup_loc_id || ">" || dropoff_loc_id as ride_route,
        count(*) as rides_count
        from trip_data
        group by
        pickup_loc_id || ">" || dropoff_loc_id
    '''
    )
trips_db.commit
trips_db.close

2 个答案:

答案 0 :(得分:0)

我将代码的最后两行更改为此:

trips_db.commit()
trips_db.close()

感谢@thesilkworm

答案 1 :(得分:-1)

能否在sqlite中编写存储过程:

然后尝试使用Python:

cur = connection.cursor()
cur.callproc('insert_into_route_agg_data', [request.data['value1'], 
request.data['value2'], request.data['value3'] ] ) 
results = cur.fetchone()  
cur.close()