E
E
Evgeny Skuridin2013-12-03 17:10:27
Python
Evgeny Skuridin, 2013-12-03 17:10:27

Python and SQLite 3. Why can't insert data?

Execution does not cause errors, but there is no data in the database.

import sqlite3

db = sqlite3.connect('database.db')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS video")
sql = """
        CREATE TABLE video(
        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
        file VARCHAR(255), title VARCHAR(255))
        """
cursor.execute(sql)

data = [{'filename': '1.mp4', 'title': 'MegaTitle'}, {'filename': '2.mp4', 'title': 'MegaTitle2'},]

for video in data:
    cursor.execute("INSERT INTO video(file, title) VALUES(:filename, :title)", video)
cursor.close()
db.close()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Skuridin, 2013-12-03
@skuridin

It's decided. You need to commit the changes before closing the connection.

db.commit()
db.close()

A
Alexey, 2013-12-03
@ZeLib0ba

After execute'ov forgot the entry
cursor.commit ()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question