面试概述三

原创
小哥 3年前 (2022-11-16) 阅读数 89 #大杂烩

一、python如何连接mysql数据库

  1. 先安装pymysql模块
  2. 导入pymysql模块:import pymysql
  3. 连接数据库
  4. 创建游标对象
  5. 对数据库进行增删改查
  6. 关闭游标
  7. 关闭连接

1、连接数据库

import pymysql
    db=pymysql.Connect(
    host="localhost",
    port=3306,
    user="root",
    password="123456",#mysql密码
    db="数据库名称",
    charset="utf8")
# autocommit=True,    # 如果插入数据,, 是否自动提交? 和conn.commit()功能一致。

python, 必须有一个游标对象, 用来给数据库发送sql语句, 并执行的.

  1. 创建游标对象
    cur = db.cursor()
    3、创建数据库表

    try:
    create_sqli = "create table goods (id int, name varchar(30));"
    cursor.execute(create_sqli) except Exception as e:
    print("创建数据表失败:", e) else: print("创建数据表成功;")

4、在数据库表中添加数据

try:  
    insert_sqli = "insert into goods values(2, fensi);"          
    cursor.execute(insert_sqli)
except Exception as e:    
    print("插入数据失败:", e)
else:    
    # 如果是插入数据, 一定要提交数据, 不然数据库中找不到要插入的数据;    
    db.commit()    print("插入数据成功;")

5、查询数据库表

sqli = "select * from goods;"
result = cursor.execute(sqli)  # 默认不返回查询结果集, 返回数据记录数。

6、

print(cursor.fetchone())     # 1). 获取下一个查询结果集;,有几个这个查询几个从下一个开始查询,查过就不看了
print(cursor.fetchone())

7、

print(cursor.fetchmany(4))   # 2). 获取制定个数个查询结果集;
info = cursor.fetchall()     # 3). 获取所有的查询结果

8、

#  5). 移动游标指针
print(cursor.fetchmany(3))
cursor.scroll(0, absolute)#正在移动指针到最开始
print(cursor.fetchmany(3))
print(cursor.fetchall())    # 移动到最后
cursor.scroll(-2, mode=relative)#正在移动指针到倒数第2个
print(cursor.fetchall())

cursor.close()#关闭游标

db.close()#关闭连接

人脸识别性别年龄

怎么看图片上的人是男是女,还有年龄
答:第三步:通过post请求,将图片传递给百度API服务器
调用百度API来识别,返回json包含性别年龄,解析出结果

第一步,获取access_token
第二步,将图片处理成base64编码格式------>将图片读取为二进制格式,再利用二进制到base64格式的函数转换

数据库表生成静态HTML

数据库表生成静态HTML
很多城市都进行积分落户,例如北京,天津等城市。

假定在数据库中存放5000条甚至更多的记录,领导要求让它生成静态的HTML页面,用户可以查看。

要求:
1,每页展示50条记录,
2,有前后翻页链接,也有首页和末页链接。
3,第一页,不需要首页链接,也不需要前一页链接。 末页不能有末页链接和后一页链接。
4,身份证号码展示在页面上的时候,其后两位使用**替代。

假定数据库表为people, 包含以下字段:
name 姓名
male 性别
id_card 身份证号
address 联系地址
score 得分
请首先创建people表,然后实现将数据库内容转换为静态HTML

完整代码:

import pymysql
def find_infor(db_name,ql_name,page_num):
    db=pymysql.Connect(host="localhost",port=3306,user="root",password="123456",db=db_name,charset="utf8")
    cursor=db.cursor()
    ql=select * from %s %ql_name
    #执行SQL语句
    cursor.execute(ql)
    result = cursor.execute(ql)
    #num网页名称编号的起始
    num=0
    result_cut=result//5+1
    result_end=result//5+1
    i=0
    while result_cut: 
        result_cut-=1
        num=num+1
        GEN_HTML =str(num) + ".html"
        f = open(GEN_HTML,w,encoding=UTF-8)
        message1 = """
        
            
                
                    
                    信息
    
    
    
          
	
                
                
                    
        """
        f.write(message1)
        str5 = str(num-1)
        str6 = str(num+1)
        #如果能取到数据则开始构造网页 
        for i in range (0,page_num):
            # if row:
            row = cursor.fetchone()
            if row:                
                # 打开文件,准备写入
                # 准备相关变量
                str0 = str(row[0])          
                str1 = str(row[1])           
                str2 = str(row[2][:16]+**)            
                str3 = str(row[3])            
                str4 = str(row[4])               
        # 写入HTML界面中
                message2 ="""
                                       
                """% (str0,str1,str2,str3,str4)

                f.write(message2)
        str7=int(result_end)
        if int(str5) == 0 :
            message3 ="""        
                    
姓名 性别 身份证号 联系地址 得分
%s %s %s %s %s
下一页 尾页 """ % (str6,str7) elif not row : message3 =""" 首页 上一页 """ % (str5) else: message3 =""" 首页 上一页 下一页 尾页 """ % (str5,str6,str7) f.write(message3) # 关闭文件 f.close() db.close()#关闭数据库连接 if __name__ == "__main__": # find_infor("information",ql_name="people",page_num=5) find_infor("information",ql_name="people",page_num=4)
  1. import pymysql

  2. 连接数据库
    db=pymysql.Connect(host="localhost",port=3306,user="root",password="123456",db="information",charset="utf8")

  3. 创建游标对象 cursor=db.cursor()

  4. 对数据库进行查找操作 ql=select * from people

  5. #执行SQL语句
    cursor.execute(ql)

  6. result = cursor.execute(ql) ,总共有多少条数据

  7. 先把表头信息输出,然后循环一条记录输出一个tr, 到50条后,再输出table的结尾部分, 最后输出html结尾部分。

  8. 关闭文件

    f.close()
    db.close() #关闭数据库连接

  9. 那把这个功能封装为函数,函数参数有三个,分别为数据库名,表名和每页记录个数。
    def find_infor(db_name,ql_name,page_num):
    ql=select * from %s %ql_name

    if name == "main":

    find_infor("information",ql_name="people",page_num=5)

    find_infor("information",ql_name="people",page_num=4)
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除