2.where 字段名 in(值1,值2,….)
3.where 字段名 not in (值1,值2,…)
空:where name is null
非空:where name is not null
NILL:空值,只能用is或is not取匹配
“” : 空字符串,用 = 或 != 去匹配
模糊比较:
where 字段名 like 表达式
表达式
_ : 匹配单个字符
%:匹配0到多个字符
NULL不会被统计
排序:order by ASC | DESC
显示: limit 开始显示位置,条数
每页显示n条记录,显示第m页:
limit(m-1)*n,n
MySQL 与 Python 交互
# mysqlpython.py
# 导入mysql模块
from pymysql import *
class MysqlPython:
def __init__(self, database, # 库
host="127.0.0.1", # ip地址
user="root", # 用户名
password="123456", # 密码
port=3306, # 端口
charset="utf8"): # 字符集
self.host = host
self.database = database
self.user = user
self.password = password
self.port = port
self.charset = charset
def open(self): # 创建数据库链接函数
self.db = connect(host=self.host,
database=self.database,
user=self.user,
password=self.password,
port=self.port,
charset=self.charset)
self.cur = self.db.cursor() # 创建游标对象
def close(self): # 创建断开数据库链接 关闭游标函数
self.cur.close()
self.db.close()
def zhixing(self, sql, L=[]): # 创建pymysql.execute() 方法函数
try:
self.open() # 链接数据库
self.cur.execute(sql, L) # 参数化执行SQL命令
self.db测试数据mit() # 提交数据
print("ok")
except Exception as e:
self.db.rollback() # 出错取消提交
print("Failed", e)
self.close() # 断开数据库链接 关闭游标
def all(self, sql, L=[]):
try:
self.open()
self.cur.execute(sql, L)
result = self.cur.fetchall()
return result
except Exception as e:
print("Failed", e)
self.close()数据库用户登录
from mysqlpython import Mysqlpython
from hashlib import sha1
uname = input("请输入用户名:")
pwd = input("请输入密码:")
# 用sha1给pwd加密
s1 = sha1() # 创建sha1加密对象
s1.update(pwd.encode("utf8")) # 指定编码
pwd2 = s1.hexdigest() # 返回16进制加密结果
sqlh = Mysqlpython("db4")
select = "select password from user where
username=%s;"
result = sqlh.all(select, [uname])
# print(result)
# (('7c4a8d09ca3762af61e59520943dc26494f8941b',),)
if len(result) == 0:
print("用户名不存在")
elif result[0][0] == pwd2:
print("登录成功")
else:
print("密码错误")ORM sqlalchemy框架
# 创建一张表 # 连接数据库的模块 from
sqlalchemy import create_engine fromsqlalchemy.ext.declarative
import declarative_base from sqlalchemy import Column, Integer
,String engine = create_engine("mysql+pymysql://root:123456@localhost/db4",
encoding="utf8") Base = declarative_base() # orm基类 class User(Base):
# 继承Base基类 __tablename__ = "t123" id =Column(Integer, primary_key=True)
name = Column(String(20)) address = Column(String(40))Base.metadata.create_all
(engine)以上就是Python全栈介绍MySQL 数据库的详细内容!
查看更多关于Python全栈介绍MySQL 数据库的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did93137