-
打开MySQL 8.0
-
建库
create database db_test;
,db_test为自己命名的库名 -
使用库
use db_test
-
检查是否建好
show databases;
-
建表
CREATE TABLE student (Sno CHAR(5) NOT NULL UNIQUE, Sname CHAR(20), Ssex CHAR(2));
-
插入两个元组
INSERT INTO student VALUES ('001','张三','男');
INSERT INTO student VALUES ('002','李四','女');
-
检查表结构
desc student;
-
检查插入
select Sname From student;
-
-
打开PyCharm新建python文件
-
在终端依次输入
pip install mysql pip install pymysql pip install mysqlclient
-
输入
import MySQLdb db = MySQLdb.connect("localhost","root","1234","db_test",charset="utf8") #("localhost","用户名","密码","库名",charset="utf8") cursor = db.cursor() cursor.execute("SELECT * from student") #("SELECT * from 表名") data = cursor.fetchall() for d in data: print(d) db.close()
-
测试
-
用Python连接MySQL
最新推荐文章于 2023-11-29 21:06:27 发布