1.导包
2.建立数据库链接
3.查询数据
4.关闭链接
# !/usr/bin/env python# _*_ coding:utf-8 _*_# 1.导包import pymysqltry:# 2.连接mysql数据库的服务connc = pymysql.Connect(# mysql服务端的IP 默认127.0.0.1/localhost-真实IPhost='192.168.9.223',user='root',password="ablejava",database='db_ai_course',port=3306,charset='utf8')# 3.创建游标对象cur = connc.cursor()# 4.编写SQL语句# 编写增加# 增加数据sql ="""INSERT INTO `tbl_demo` (`name`, `age`) VALUES (%s, %s);"""add_data = ['刘德华',11]# 5.使用游标对象执行SQLcur.execute(sql, add_data)sql = 'select * from tbl_demo;'# 5.使用游标对象去调用SQLcur.execute(sql)# 6.获取查询的结果 --print()result = cur.fetchall()if result:for item in result:print(item)# 7.关闭游标对象cur.close()# 8.关闭连接connc.close()except Exception as e:print(e)
