Created this program to insert train number and name into a database. The names and numbers are right(as the commented out print statement proves it) but the db file is empty when I open it with db reader.
Code:
import sqlite3
import re
conn=sqlite3.connect('example.db')
c=conn.cursor()
c.execute('''CREATE TABLE train
(number text, name text)''')
f=open("train.htm","r")
html=f.read()
num=re.findall(r"(?<=> )[0-9]+", html) #regex to get train number
name=re.findall(r"(?<=<font>)[A-Za-z]+[ A-Za-z]+",html) #regex to get train name
j=8
for i in range(0,len(num)):
#print(num[i],name[j]) #this statement proves that the values are right
c.execute("INSERT INTO train VALUES (?,?)",(num[i],name[j]))
j=j+3
conn.close()
But when I tried to read this database then its empty.
Code to read db:
import sqlite3
conn=sqlite3.connect('example.db')
c=conn.cursor()
for row in c.execute('SELECT * FROM train'):
#the program doesn't even enter this block
print(row)
I tried opening this databse in sqlitebrowser just to make sure, still its empty so there is something wrong with my first program which is unable to insert values. Why so?