1、创建python容器
- 创建空文件夹
mkdir lab5
- 文件目录如下
.
├── connect.py
├── create.py
├── cvvv.py
├── date.py
├── delete1.py
├── Dockerfile
├── hello.py
├── insert.py
├── requirements.txt
├── select1.py
└── test.jpg
- requirements.txt(用于安装依赖)
PyMySQL
opencv-python
- Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt -i https://pypi.douban.com/simple
ENTRYPOINT [ "python" ]
CMD ["hello.py" ]
- sudo docker bulid -t docker/python
2、python的部署运行
- helloworld
print('hello world')
sudo docker run --rm -v /home/lzh/桌面/lab5:/usr/src/app docker/python
- 日历
import calendar
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
print(calendar.month(yy,mm))
sudo docker run -it --rm -v /home/lzh/桌面/lab5:/usr/src/app docker/python date.py
3、python+数据库操作
sudo docker start cdfdc82c5ef7
- 连接
import pymysql
# 打开数据库连接
db = pymysql.connect("192.168.136.133","root","admin","mysql" )
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print ("Database version : %s " % data)
# 关闭数据库连接
db.close()
- 创建
# 打开数据库连接
db = pymysql.connect("192.168.136.133","root","admin","mysql" )
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
# 使用预处理语句创建表
sql = """CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""
cursor.execute(sql)
# 关闭数据库连接
db.close()
- 插入
import pymysql
# 打开数据库连接
db = pymysql.connect("192.168.136.133","root","admin","mysql" )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 插入语句
sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('lzh', '111', 20, 'M', 2000)"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 如果发生错误则回滚
db.rollback()
# 关闭数据库连接
db.close()
- 查询
import pymysql
# 打开数据库连接
db = pymysql.connect("192.168.136.133","root","admin","mysql" )
cursor = db.cursor()
# SQL 查询语句
sql = "SELECT * FROM EMPLOYEE
WHERE INCOME > %s" % (1000)
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# 打印结果
print ("fname=%s,lname=%s,age=%s,sex=%s,income=%s" %
(fname, lname, age, sex, income ))
except:
print ("Error: unable to fetch data")
# 关闭数据库连接
db.close()
- 删除
import pymysql
# 打开数据库连接
db = pymysql.connect("192.168.136.133","root","admin","mysql" )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 删除语句
sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (19)
try:
# 执行SQL语句
cursor.execute(sql)
# 提交修改
db.commit()
except:
# 发生错误时回滚
db.rollback()
# 关闭连接
4、opencv
-
找一张图片
-
gedit cvvv.py
#查看图片像素的BGR值
import cv2
img=cv2.imread('test.jpg')
px=img[100,100]
print(px)
blue=img[100,100,0]
print(blue)
5、遇到的错误和解决方案
- 添加参数it
- 文件名字冲突,修改文件名即可
6、时间花费
步骤 | 时间(min) |
---|---|
docker | 10 |
python | 10 |
python+mysql | 20 |
opencv | 15 |
博客 | 20 |