Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate database in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to operate the database in python, the content is concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.

Python3 connection mysql requires installation of the mysql module

1. Local installation: pycharm version 2019.3;python version 3.7.3;pymysql version 0.10.0;

2. Remote installation: Install centos7 system in virtual machine; install mysql version 5.7.23 in centos7 system;

Python interface to database

pymysql.Connect() Parameters

host(str): MySQL server address

port(int): MySQL server port number

user(str): username

passwd(str): Password

db(str): database name

charset(str): connection encoding

Connection object supported methods

cursor() creates and returns a cursor using this connection

commit() commits the current transaction

rollback() Rolls back the current transaction

close() Close the connection

Cursor object supported methods

execute(op) Executes a database query command

fetchone() gets the next line of the result set

fetchmany(size) Gets the next few lines of the result set

fetchall() fetches all rows in the result set

rowcount() Returns the number of data or affected rows

close() Closes the cursor object

PS: If you need Python learning materials, you can add the group below to find a free administrator to receive

Click on the group to get free Python learning materials

You can receive source code, project actual video, PDF files, etc. for free

Python入门基础:如何使用python操作数据库

#Create cursor conn = pymysql.connect(host='192.168.126.131', port=3306, user='root', password='123456', ) print(conn) cursor = conn.cursor() print(cursor) output: #Cursor object #If you want to know what properties and methods the cursor has, you can view everything defined by Cursor class in cursors.py file #View the version information of the connected database, using execute() and fetchall() methods defined by cursor class conn = pymysql.connect(host='192.168.126.131', port=3306, user='root', password='123456', ) cursor = conn.cursor() cursor.execute ("select version()") data = cursor.fetchall() print ("Mysql's version : %s " % data) output: Mysql's version : ('5.7.23',) # mysql version is 5.7.23 #Create databases and tables using sql statements or database tools navicat; CREATE DATABASE database_name; CREATE TABLE table_name (column_name column_type); The above content is how to operate the database in python. Have you learned knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report