sakuto29 / pyodbc

Automatically exported from code.google.com/p/pyodbc
MIT No Attribution
0 stars 0 forks source link

Cannot insert record into MS Access DB #241

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Im trying to add record to the database. There is code i have:

import pyodbc, os

database = os.getcwd()+'\\DB.accdb'
if os.path.exists(database):
    print 'DB exists!'
else:
    print 'Database not found!!!'
    exit()

constr = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=%s;'  % database
conn = pyodbc.connect(constr)
cur = conn.cursor()

sql = "INSERT INTO clients_test 
(name,mailTo,mailCC,mailBCC,mailbox,description) VALUES (1,2,3,4,5,6)"
cur.execute(sql)

sql = "select * from clients_test"
cur.execute(sql)

result = cur.fetchall()
for item in result:
    print item

cur.close()
conn.close()

console output:
DB exists!
(47, u'1', u'2', u'3', u'4', u'5', u'6')

but when i open the database, the database is empty

Original issue reported on code.google.com by ramax...@gmail.com on 27 Feb 2012 at 6:30

GoogleCodeExporter commented 8 years ago
Autocommit defaults to False so you need to set it to True to keep your changes.

Original comment by davidp.r...@gmail.com on 29 Feb 2012 at 2:15

GoogleCodeExporter commented 8 years ago
Try this

sql = ("INSERT INTO client_test 
(name,mailTo,mailCC,mailBCC,mailbox,description) VALUES(1,2,3,4,5,6);")
cur.execute(sql)
cur.commit(sql)

Original comment by pyneha...@gmail.com on 14 Sep 2012 at 5:31

GoogleCodeExporter commented 8 years ago

Original comment by mkleehammer on 29 Sep 2012 at 5:00