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

Simple operation of adding, deleting, modifying and querying Python3 database

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

Share

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

1, add a table using Python

# Import the module used to operate the database

Import pymysql

# establishing a connection database object

Conn=pymysql.connect (host='127.2.2.2',user='root',passwd='123456',db='records')

# create a cursor

Cur=conn.cursor ()

# execute the sql statement with the method in the cursor

Cur.execute ("create table people (name char (20), height int (3), weight int (3)")

# close the cursor after the statement is executed

Cur.close ()

# close the connection to the database

Conn.close ()

The above code can be viewed in the database after execution, and a table of people has been created.

2, insert a piece of data in the table.

# execute the sql statement with the method in the cursor

Cur.execute ("insert into people values ('tom',110,34)")

# submit the insert operation you just did to make it effective

Conn.commit ()

3. Modify a piece of data in the table to change height to 177,

# execute the sql statement with the method in the cursor

Cur.execute ("update people set height=177 where name='tom'")

# submit the insert operation you just did to make it effective

Conn.commit ()

4, query the data in the table

# execute the sql statement with the method in the cursor

Cur.execute ("select * from people")

The # fetchall method can capture the result of the statement executed by select above.

Print (cur.fetchall ())

5. Delete the data in the table

# execute the sql statement with the method in the cursor

Cur.execute ("delete from people where name='tom'")

# submit the insert operation you just did to make it effective

Conn.commit ()

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

Database

Wechat

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

12
Report