Skip to content Skip to sidebar Skip to footer

How To Insert Dynamic Data In Sqlite3 Using Python

enter code here import sqlite3 conn = sqlite3.connect('tutorial.db') c = conn.cursor() def create_table(): c.execute('CREATE TABLE example(Language VARCHAR, Version REAL, Skill

Solution 1:

The message indicates that the table example does not exist. You do not appear to be calling create_table (or enter_data)

Try using :-

import sqlite3
conn = sqlite3.connect('tutorial.db')
c = conn.cursor()


def create_table():
    c.execute("CREATE TABLE IF NOT EXISTS example(Language VARCHAR, Version REAL, Skill TEXT)") #<<<<<<<<<< CHANGED


def enter_data():
    c.execute("INSERT INTO example VALUES('Python', 2.7, 'Beginner')")


create_table() #<<<<<<<<<< ADDED
enter_data() #<<<<<<<<<< ADDED
c.execute("INSERT INTO example VALUES('Python', 3.3, 'Intermediate')")
c.execute("INSERT INTO example VALUES('Python', 3.4, 'Expert')")
conn.commit()


def enter_dynamic_data():
    lang = input("What language? ")
    version = float(input("What version? "))
    skill = input("What skill level? ")
    c.execute("INSERT INTO example (Language, Version, Skill) VALUES (?, ?, ?)",
          (lang, version, skill))
    conn.commit()
enter_dynamic_data()

cursor = c.connection.cursor() #<<<<<<<<<< ADDED
cursor.execute("SELECT * FROM example") #<<<<<<<<<< ADDED
for row in cursor: #<<<<<<<<<< ADDED
    print("Language=", row[0], " Version=", row[1], " Skill=", row[2]) #<<<<<<<<<< ADDED

conn.close()

The above results in :-

What language? Basic
What version? 3
What skill level? Easy
Language= Python  Version= 2.7  Skill= Beginner
Language= Python  Version= 3.3  Skill= Intermediate
Language= Python  Version= 3.4  Skill= Expert
Language= Basic  Version= 3.0  Skill= Easy

Process finished with exit code 0

Post a Comment for "How To Insert Dynamic Data In Sqlite3 Using Python"