以靶向的方式掌握Python的各种知识点
还可以充分准备企业的面试
验证自己对Python的各种知识的掌握情况
你将收获
以靶向的方式掌握Python的各种知识点
还可以充分准备企业的面试
验证自己对Python的各种知识的掌握情况
适用人群
课程介绍
本课知识点
同学笔记
2020-11-09 16:48:35
题一:如何创建MySQL数据库
pymysql
pip install pymysql
from pymysql import *
def connectDB():
db = connect('127.0.0.1','root','1234567','meituan',charset='utf-8')
return db
print(type(db))
def createTable(db):
cursor = db.cursor()
c = db.cursor()
try:
c.execute('''create table persons
(id int primary key not null,
name text not null,
age int not null,
address char(100),
salary real);''')
db.commit()
except:
db.rollback()
return False
if createTable(db):
print('create table sucess')
else:
print('create table failed')
题二:如何向MySQL表中插入数据库
def insertRecords(db):
cursor = db.cursor()
try:
except Exception as e:
print(e)
db.rollback()
题三:如何查询MySQL的数据
2020-11-08 23:03:26
题一:如何创建SQLite数据库
# create table and database
import sqlite3
import os
dbPath = 'data.sqlite'
if not os.path.exists(dbPath):
conn = sqlite3.connect(dbPath)
c = conn.curson()
c.execute('''create table persons
(id int primary key not null,
name text not null,
age int not null,
address char(100),
salary real);''')
conn.commit()
conn.close()
print('创建数据库成功')
题二:如何向SQLite表中插入数据
#2.insert
conn = sqlite3.connect(dbPath)
c.execute('delete from persons')
c.execute(''' insert into persons(id,name,age,address,salary)values(1,'Bill',32,'California',20000)''')
c.execute(''' insert into persons(id,name,age,address,salary)values(2,'Mike',30,'texas',10000)''')
c.execute(''' insert into persons(id,name,age,address,salary)values(3,'John',54,'Norway',30000)''')
conn.commit()
print('insert success')
题三:如何查询SQLite表中的数据
persons = c.execute('select name, age,address,salary from persons order by age')
print(type(persons))
result = []
for person inpersons:
value = {}
value['name'] = person[0]
value['age'] = person[1]
value['name'] = person[2]
result.append(value)
conn.close()
print(type(result))
print(result)
import json
resultStr = json.dumps(result)
print(resultStr)
2020-11-08 23:00:28
题一:将一个对象转化为对应的JSON字符串
import json
class Product:
def __init__(self,name,price,count):
self.name = name
self.prince = price
self.count = count
product = Product('特斯拉',1000000,20)
def product2Dict(obj):
return {
'name':obj.name
'price':obj.price
'count':obj.count
}
jsonStr = json.dumps(product,default=product2Dict,ensure_ascii=False)
print(jsonStr)
题二:将对象列表转换为JSON数组
f = open('file/products.json','r',encoding='utf-8')
jsonStr = f.read()
class Product:
def __init__(self,d):
self.__dict__ = d
products = json.loads(jsonStr,object_hook=Product)
print(products)
for product in products:
print(product.name)
jsonStr = json.dumps(products,default=product2Dict,ensure_ascii=False)
print(type(jsonStr))
总结:json模块的dumps用于将对象转换为JSON字符串,通过default参数指定一个转换函数,可以在该函数中提取对象的属性值,并生成JSON对象,最后dumps负责将转换函数返回的JSON对象转换为JSON字符串。
没有更多了
课程讨论
Kelly糖�
来源:导入Python模块
machine_learning_探界者
来源:设置Python模块搜索路径有几种方式,如何使用
qq_32247623
来源:快速调换字典中的key和value
cedrus
来源:找到第n个丑数
cedrus
来源:乘积最大子序列
吴千行
成长,奋斗ing!
来源:导入Python模块
qq_47320003
来源:导入Python模块
mamba0526
来源:小矩形覆盖大矩形
Nelson-M
来源:del和pop在删除列表元素上有什么区别