搞一个界面实时记录的表格(十分简单!!!)

知识点讲解

pymysql  (MySQLdb)

# pymysql.install_as_mysqldb()  #兼容性命令

pymysql是python操作MySQL数据库的第三方模块

安装方法

1.Pip install pymysql 常规安装

pip install pymysql -i 国内的源

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

导出pip清单

Pip freeze > package.txt

批量安装

Pip install -r package.txt

2、zip安装包


Python setup.py install

C++版本错误

3、Whl包

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

1、pip install wheel

4、pycharm

pymysql 使用基本流程

要求

1 要有mysql数据库

2 要有python

3 要有pymysql

流程操作

1 创建数据库连接

2 创建游标

3 执行命令 

4 关闭连接

import pymysql

#创建数据库链接
db = pymysql.connect(
    host = "localhost", #主机ip
    user = "root", #数据库用户
    password = "1111", #用户对应的密码
    database = "school", #对应的数据库
    port = 3306, #数据库端口,默认3306
    charset = 'utf8' #数据库编码
)
#创建游标:游标用于传递python给mysql的命令和mysql返回的内容
cursor = db.cursor()
#执行部分
exe = cursor.execute("show databases") #执行命令,返回查询的条数
print(exe)
#result = cursor.fetchone()
#result = cursor.fetchmany(3)
result = cursor.fetchall() #查询结果

print(result)
#关闭部分
db.commit() #链接提交,用于对数据库的增删改
cursor.close() #关闭游标
db.close() #关闭链接

CGI

通用网关接口,用来解析后端脚本。

Python 自带轻量级的cgi web服务器,我们在对应目录下可以通过命令进行启动

Python -m http.server --cgi [port] 端口默认8000

通常服务器会自动解析根目录下的index.html文件,所以我们可以默认访问到index.html

在浏览器当做cgi的ip+端口代表根目录

cgi可解析python脚本作为html

1 需要在cgi的根目录下创建cgi-bin目录

 2、需要用print的方法来返回HTML的头信息
print("Content-type:text/html;charset:utf8") #响应头
#content-type:返回内容的类型
    # text/html文本
    # application/json json文本
    # image/jpg jpg图片
print("\n") #声明响应头结束
print(html) #返回内容的正文
    3、cgi.FieldStorage可以接收前端传递过来的get或者post请求的数据,我们需要用getvalue方 法通过前端form表达的name来取到input的值。

猜你喜欢

转载自blog.csdn.net/weixin_44303465/article/details/88562548