[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sqlite3 table, insert values from python dictionary

Posted on 2014-01-21
2
Medium Priority
?
2,150 Views
Last Modified: 2014-01-22
I have created the following sqlite3 database table from python:

sqlite> .schema my_table
CREATE TABLE my_table ( myID INTEGER PRIMARY KEY NOT NULL,            
                       Dtg TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,            
                       Date VARCHAR(40),            
                       Holiday VARCHAR(128),            
                       Service VARCHAR(20),            
                       Sign_Up VARCHAR(40),            
                       Last_Name VARCHAR(128),            
                       First_Name VARCHAR(128),            
                       Phone VARCHAR(40),            
                       Email VARCHAR(128));

Open in new window


but I cannot insert values into the table.  Here is my commented code:

layMin = {'myID':'','Dtg':'', 'Date':'2014-02-18', 'Holiday':'', 'Service':'0:900 AM',\
          'Sign_Up':'Eucharistic Minister', 'Last_Name':'Voyage', \
          'First_Name':'Maiden', 'Phone':'444-5555', 'Email':'maiden@voyage.com' }


sql_insert = ("INSERT into my_table(myID,Dtg,Date,Holiday,Service,Sign_Up,\
Last_Name,First_Name,Phone,Email) VALUES(layMin['myID'],layMin['Dtg'],\
layMin['Date'],layMin['Holiday'],layMin['Service'],layMin['Sign_Up'],\
layMin['Last_Name'],layMin['First_Name'],layMin['Phone'],layMin['Email']);")
    
try:
     con = lite.connect(db_path)
     cursor = con.cursor()  # I can open the database
     cursor.execute(sql_insert) # but this fails
     con.commit()
     con.close()   

except lite.Error, e:

    print "Error could not connect to database"; #this is the response
    sys.exit(1) 

Open in new window


 I've had no trouble inserting the same records into a mysql database, but my new host doesn't have that application.  Help.
0
Comment
Question by:sara_bellum
2 Comments
 
LVL 17

Accepted Solution

by:
gelonida earned 2000 total points
ID: 39799322
I would add a print statement in line 10
print sql_insert

if you have the command line version of sqlite3 (exists under linux and windows) you could try to copy and paste the line into sqlite3 ind you should easier see the error.


In your particular case the escaping and replacement is quite wrong.
I'm rather sure the same line will also not work ander mysql.

sql_insert = "INSERT into my_table(myID),  VALUES(%s)" % layMin['myID']

would probably work better though this would be prone to sql injection.

If I remember, the way to avoid sql injection with sqlite is

sql_insert = "INSERT into my_table(myID),  VALUES(?)"

sql_values = (layMin['myID'], )

cursor.execute(sql_insert, sql_values)
0
 

Author Comment

by:sara_bellum
ID: 39801464
Thanks Gelonida, I had forgotten to paste my commands at the sqlite> prompt to yield syntax errors.  I tried many, and ran out of patience with dictionary values so I reverted to working with a list. I post this in case it's useful to someone:

cursor.execute('insert into table_name (field3, field4, field5...field10) values  (?,?,?,?,?,?,?,?)', value_list)

Open in new window


One should not include blank fields in the value list to account for autoincrement or sql timestamps since those are generated by sqlite.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
What we learned in Webroot's webinar on multi-vector protection.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question