AlexW
AlexW

Reputation: 2589

sqlite python - insert into table not executing

im running a loop to insert some data into a table, i can see that the query looks correct but when i run another script to show me rows in the table its emtpy

Script to create queries and execute them

objPolicyData = getUserData()
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()

for UserItem in objPolicyData['users']:     
    pdID = UserItem['id']
    pdJobTitle = UserItem['job_title']
    pdPolicy = ''
    if pdJobTitle == 'Network Engineer':
        pdPolicy = 'Network Team'
    elif pdJobTitle == 'System Administrator':
        pdPolicy = 'Sysadmin Team'
    elif pdJobTitle == 'Infrastructure Engineer':
        pdPolicy = 'Infrastructure Team'
    elif pdJobTitle == 'Database Administrator' and 'oracle' in UserItem['description'].lower():
        pdPolicy = 'Oracle DBA Team'    
    elif pdJobTitle == 'Database Administrator' and 'mysql' in UserItem['description'].lower():
        pdPolicy = 'MySQL DBA Team' 

    query = "INSERT INTO oncall_pduser (Name,Mobile,PagerDutyID,PagerDutyPolicy) VALUES ('%s','%s','%s','%s');" % (UserItem['name'],getUserMobile(pdID),pdID,pdPolicy)
    print query
    c.execute(query)

script to check the table

import sqlite3
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("SELECT * from oncall_pduser")
for row in c.fetchall():
    print(row)

sample of query printed from script

INSERT INTO oncall_pduser (Name,Mobile,PagerDutyID,PagerDutyPolicy) VALUES ('John Smith','0123 12 4596','159842','Network Team');

sample of pulling the table

[root@network-tools infternal]# python test.py
(1, u'test', u'1235466', u'555', u'BOB')

theres only the one test user that i added using the django admin page

Thanks

Upvotes: 0

Views: 39

Answers (1)

AlexW
AlexW

Reputation: 2589

i needed to commit the changes!

conn.commit()

Upvotes: 1

Related Questions