Python MySQL Insert Not Functioning
In Python, utilizing the MySQL API to connect to a MySQL database is a popular approach. However, difficulties might arise when attempting to insert records into the database.
One such issue that has been encountered is the inability to insert records. Upon examination, the code involved in the insertion operation appears as follows:
db = MySQLdb.connect("localhost","root","padmaramulu","pdfsearch") cursor = db.cursor() temp = "hello";number = 2; cursor.execute('insert into documents(docid,docname) values("%d","%s")' % (number,temp)) db.close()
The reason for this issue lies in the lack of a commit operation. After executing the insertion statement, it is crucial to commit the changes to the database using db.commit(). Failure to do so prevents the database from permanently storing the inserted records.
To rectify this issue, the following line should be added after the insertion statement:
db.commit()
By incorporating this line, the changes made to the database are committed, ensuring the successful insertion of records.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3