?
Solved

Sqlite3 table, insert values from python dictionary

Posted on 2014-01-21
2
Medium Priority
?
2,004 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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