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 密码为空。
本文档主要讲述的是android rtsp流媒体播放介绍;实时流协议(RTSP)是应用级协议,控制实时数据的发送。RTSP提供了一个可扩展框架,使实时数据,如音频与视频,的受控、点播成为可能。数据源包括现场数据与存储在剪辑中数据。该协议目的在于控制多个数据发送连接,为选择发送通道,如UDP、组播UDP与TCP,提供途径,并为选择基于RTP上发送机制提供方法。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看
0
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号