Python中mysql命令安裝


pymysql是python中操作mysql的模塊

安裝mysql
pip install pymysql
mysql版本:mysql-installer-community-8.0.12.0
基本命令
創建數據庫 create database student;
使用數據庫 use student;
顯示所有表 show tables;

import pymysql
#1、創建數據庫連接對象
connect = pymysql.connect(
# host:表示主機地址
# 127.0.0.1 本機ip
# 172.16.21.41 局域網ip
# localhost (local:本地 host:主機 合在一起為本地主機的意思)
# host表示mysql安裝的地址
host=“127.0.0.1”,
user=“root”,
passwd=“123456”,
# mysql默認的端口號是3306
port=3306,
# 數據庫名稱
db=“student”
)

#2、創建游標,用於操作表
cursor = connect.cursor()

3、創建表
create_table = “create table if not exists stu (name varchar(30), age integer, phone varchar(11))”
cursor.execute(create_table)


#4、表的增、刪、改、查
#增加
insert_table = “insert into stu (name,age,phone) values (‘張三’, 19, ‘13332001256’)”
cursor.execute(insert_table)

#刪除
delete_table = “delete from stu where name=‘張三’”
cursor.execute(delete_table)

#修改
update_table = “update stu set age = 20 where id < 10”
cursor.execute(update_table)

#查詢
select_table = “select * from stu”
res = cursor.execute(select_table)

s = res.fetchone()
print(s)

ss = res.fetchall()
print(ss)

5、提交sql語句
connect.commit()
6、關閉游標、數據庫
cursor.close()
connect.close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM