Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to connect to SQLITE database and it would seem that the code does create the database, but however it does not create the tables nor does it insert anything in them (most probably because they were never created)

I have the following code:
Python
import sqlite3
conn = sqlite3.connect("dfg.db")
c = conn.cursor()

def create_tabe():
    c.execute('CREATE TABLE IF NOT EXISTS tabl(city TEXT, temp REAL)')

def data_entry():
    c.execute("INSERT INTO tabl VALUES ('dasfsd', 32434)")
    conn.commit()
    c.close()
    conn.close()


What I have tried:

I have SQLite DB Browser and in it I open the database but lack any tables or anything at all. Also I see my database in the folder next to my project.
Posted
Updated 22-Nov-16 6:06am
Comments
[no name] 22-Nov-16 12:02pm    
It is inside the function definition have you made a function call?

1 solution

Python
import sqlite3
conn = sqlite3.connect("dfg.db")
c = conn.cursor()
 
def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS tabl(city TEXT, temp REAL)')
 
def data_entry():
    c.execute("INSERT INTO tabl VALUES ('dasfsd', 32434)")
    conn.commit()
    c.close()
    conn.close()


#Add this to create a table
create_table()


seems to be you have forgotten to call the function
Note I did called only the function which creates the table call the second function definition to add the data
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900