Solved

Sqlite3 table, insert values from python dictionary

Posted on 2014-01-21
2
1,838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
gelonida earned 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…
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…

730 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