How to connect to the database in python-How to connect to the database in python

Time: 2022-10-07Source: Huajun Software TutorialAuthor: Shaobing

      近日有一些小伙伴咨询小编python怎么连接数据库?下面就为大家带来了python连接数据库的方法,有需要的小伙伴可以来了解了解哦。

The first step: open the pycharm development tool, create a new python file; import the pymysql package.


Second step: Call the connect method in the pymysql module to connect to the mysql database.


Step 3: Use the cursor() method to create a cursor object, and then assign it to cur.


Step 4: Call the execute method to execute the create table t_kmn statement. First delete the table and then create it.


Step 5: Call the execute method again to execute the above SQL statement.


Step 6: Finally, call the close() method to close the database connection.


Step 7: Save the code and run the python file. After successful execution, check whether the MySQL client generates the t_kmn table.


The above is the entire content of how to connect the database in Python brought to you by the editor. I hope it can help you.

Related articles更多>>

Popular recommendations