Solved

Sqlite3 table, insert values from python dictionary

Posted on 2014-01-21
2
1,663 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 16

Accepted Solution

by:
gelonida earned 500 total points
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now