mysql - Not showing data in database after insertion using python -
i using python 2.7 , mysql database. in python program have insert
query this:
cursor.execute("insert login(username,passw)values('"+i.username+"','"+i.password+"')") result=cursor.execute("select * login") print cursor.fetchall()
when check in database, there no entry. after select in python code, when print results showing inserted data. not using transaction statement either.
you need commit transaction database make insert permanent, , need use sql parameters prevent sql injection attacks , general quoting bugs:
cursor.execute("insert login (username, passw) values (%s, %s)", (i.username, i.password)) connection.commit()
until commit, data inserted visible python program; if not commit @ all, changes discarded again database.
alternatively, switch on auto-commit mode:
connection.autocommit()
after switching on auto-commit, insertions committed instantly. careful this lead inconsistent data if need insert data multiple rows , / or tables interdependent.
Comments
Post a Comment