简单的连接MySQL与Python的Bottle框架的方法
作者:JohnnyHu90 时间:2024-01-27 23:49:40
Python关于mySQL的连接插件众多,Bottle下也有人专门开发的插件:bottle-mysql具体使用方法见官方,总共感觉其用法限制太多,其使用起来不方便,最适合的当然是,mySQL官网给Python提供的通用官方驱动,用起来很顺手:mysql-connector 具体操作如下:
# -*- coding: utf-8 -*-
#!/usr/bin/python
# filename: login_admin.py
# codedtime: 2014-9-7 11:26:11
import bottle
import mysql.connector # 导入mysql数据库连接器
def check_userinfo():
a_list = [] # 创建一个空列表
username = bottle.request.GET.get('loginname','').strip() # 用户名
password = bottle.request.GET.get('password','').strip() # 密码
if username is not None or password is not None:
try:
# 连接数据库
conn = mysql.connector.connect(user='root', password='123456', database='myblog')
cursor = conn.cursor() # 创建数据游标
# 执行查询
query = ("SELECT username, password FROM mb_users "
"WHERE username=%s and password=%s")
cursor.execute(query, (username, password))
a_list = cursor.fetchall() # fetchone获取一个元组
#count = int(cursor.rowcount) # 获取元组个数
return a_list
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))
exit()
finally:
conn.commit() # 提交修改
cursor.close() # 关闭数据库
conn.close()
else:
return a_list
def login_admin():
if bottle.request.GET.get('bs-submit','').strip(): #点击登录按钮
a_list = check_userinfo()
if a_list:
a_name = a_list[0][0] # 获得用户名
return bottle.template('templates/index_user.tpl', username = a_name)
else:
return bottle.template('templates/login_admin.tpl', action='/login_admin',
error_info='请输入正确的用户名或密码!')
else:
return bottle.template('templates/login_admin.tpl', action='', error_info=' ')
以上是MySQL在Botlle中的简单用法,
顺便提一下:安装和管理mySQL,建议安装使用XAMPP,XAMPP集成了Apache, MySQL、PHP、Tomcat等多种工具,一次性解决安装,不用自己繁琐的一个个安装和配置,而且管理也很方便。XAMPP安装的MySQL默认用户是:root 密码为空。
标签:Python
0
投稿
猜你喜欢
Div即父容器不根据内容自适应高度的解决方法
2010-04-23 18:19:00
Python实现注册登录系统
2021-10-21 20:01:05
python实现学员管理系统
2021-05-31 07:02:45
ASP给长的标题加省略号...
2008-02-22 14:39:00
vue axios二次封装的详细解析
2024-01-18 01:43:44
解密新型SQL Server无文件持久化恶意程序的问题
2024-01-17 08:34:12
Python实现霍夫圆和椭圆变换代码详解
2022-12-22 19:32:29
Anaconda3+tensorflow2.0.0+PyCharm安装与环境搭建(图文)
2021-09-28 02:54:28
解决python3.6 右键没有 Edit with IDLE的问题
2023-07-31 17:15:00
python3中的logging记录日志实现过程及封装成类的操作
2023-07-30 21:58:21
python 实现非极大值抑制算法(Non-maximum suppression, NMS)
2021-01-18 21:46:20
用Python实现屏幕截图详解
2022-01-30 08:00:19
javascript定义变量时带var与不带var的区别分析
2023-08-23 12:39:21
Python模拟登录网易云音乐并自动签到
2023-05-28 03:10:05
distinct 多列问题结合group by的解决方法
2024-01-21 11:18:23
python utc datetime转换为时间戳的方法
2021-11-18 07:11:44
关于Python中Inf与Nan的判断问题详解
2021-10-31 18:56:33
php实现通过cookie换肤的方法
2023-11-23 17:57:07
巧用Dreamweaver4文件库更新网站
2007-02-03 11:31:00
javascript跨域原因以及解决方案分享
2024-04-10 10:44:32