sara_bellum
asked on
Sqlite3 table, insert values from python dictionary
I have created the following sqlite3 database table from python:
but I cannot insert values into the table. Here is my commented code:
I've had no trouble inserting the same records into a mysql database, but my new host doesn't have that application. Help.
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));
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)
I've had no trouble inserting the same records into a mysql database, but my new host doesn't have that application. Help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.