Solved

Sqlite3 table, insert values from python dictionary

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a replacement data type in Oracle 6 77
Trigger usage 2 67
Is doing tutor.com teaching in my situation advisable? 2 83
Mysql Left Join Case 10 44
Read about achieving the basic levels of HRIS security in the workplace.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

813 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

16 Experts available now in Live!

Get 1:1 Help Now