将列表导入python sql db

时间:2017-01-17 09:47:48

标签: arrays list python-3.x sqlite

我正在尝试使用python将列表导入到sql db中。 我在互联网上搜索了答案,但我找到的解决方案对我的程序不起作用。

def create_table(db_name,sql):
with sqlite3.connect(db_name) as db:
    cursor = db.cursor()
    cursor.execute(sql)
    db.commit()

if __name__ == "__main__":
    db_name = "TVshows.db"
    sql = """create table TVPrograms
    (ShowID integer,
    Showname text,
    Primary Key(ShowID))"""


def insert_data(TVshows):
    with sqlite3.connect("TVshows.db") as db:
        cursor=db.cursor()
        count=0
        while count <5:
            ShowID=TVshows[0]
            cursor.execute("INSERT into TVprograms(Showname,ShowID) VALUES (?,?);',[','.join(TVshows)])")
            count=count+1
            ShowID=TVshows[0+1]
            db.commit()       
        conn.commit()
        cursor.close()

TVshows = ['The X Files', 'Breaking Bad', "Star Trek", "Daredevil"]`

insert_data(TVshows)

我错了。我无法弄清楚如何让它发挥作用。 我的错误信息:

Traceback (most recent call last): File "H:/Python/tvshow.py", line 32, in <module> insert_data(TVshows) 

File "H:/Python/tvshow.py", line 23, in insert_data cursor.execute("INSERT into TVprograms(Showname,ShowID) VALUES (?,?);',[','.join(TVshows)])")

sqlite3.Warning: You can only execute one statement at a time.

0 个答案:

没有答案