• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 简单的连接MySQL与Python的Bottle框架的方法

简单的连接MySQL与Python的Bottle框架的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python bottle,bottle框架,美国que bottle京东,que bottle伸缩水壶,baby bottle奶瓶等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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=' ')

</div>

    
以上是MySQL在Botlle中的简单用法,

顺便提一下:安装和管理mySQL,建议安装使用XAMPP,XAMPP集成了Apache, MySQL、PHP、Tomcat等多种工具,一次性解决安装,不用自己繁琐的一个个安装和配置,而且管理也很方便。XAMPP安装的MySQL默认用户是:root  密码为空。

</div>

您可能想查找下面的文章:

  • 关于python的bottle框架跨域请求报错问题的处理方法
  • 使用Python的Bottle框架写一个简单的服务接口的示例
  • 使用Python的Bottle框架写一个简单的服务接口的示例
  • Python的Bottle框架中返回静态文件和JSON对象的方法
  • 简单的连接MySQL与Python的Bottle框架的方法
  • Python的Bottle框架中实现最基本的get和post的方法的教程
  • Python的Bottle框架中返回静态文件和JSON对象的方法
  • 简单的连接MySQL与Python的Bottle框架的方法
  • Python的Bottle框架中实现最基本的get和post的方法的教程
  • Python的Bottle框架中获取制定cookie的教程

相关文章

  • Python读写txt文本文件的操作方法全解析
  • 使用Python的urllib2模块处理url和图片的技巧两则
  • python利用urllib实现爬取京东网站商品图片的爬虫实例
  • 从零学Python之入门(四)运算
  • Python教程之全局变量用法
  • python备份文件的脚本
  • python求crc32值的方法
  • python网络编程学习笔记(六):Web客户端访问
  • 在Python中编写数据库模块的教程
  • python中OrderedDict的使用方法详解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Django与遗留的数据库整合的方法指南
    • Using Django with GAE Python 后台抓取多个网站的页面全文
    • python实现登陆知乎获得个人收藏并保存为word文件
    • python导入时小括号大作用
    • Python中遇到的小问题及解决方法汇总
    • 寻找网站后台地址的python脚本
    • python中遍历文件的3个方法
    • python分析nignx访问日志脚本分享
    • 简介二分查找算法与相关的Python实现示例
    • Python中zfill()方法的使用教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有