国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

簡單的連接MySQL與Python的Bottle框架的方法

2019-11-25 17:34:28
字體:
來源:轉載
供稿:網友

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:11import bottleimport 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_listdef 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  密碼為空。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乡宁县| 青阳县| 莲花县| 富源县| 玉溪市| 博兴县| 小金县| 南京市| 读书| 高密市| 大冶市| 广州市| 荆州市| 青田县| 邯郸市| 滦平县| 钟山县| 孟连| 平泉县| 长泰县| 云龙县| 巢湖市| 新安县| 新疆| 德格县| 阳东县| 原平市| 余江县| 公主岭市| 上思县| 晋城| 介休市| 高尔夫| 南岸区| 德兴市| 安平县| 丹东市| 瑞金市| 平原县| 金溪县| 丰原市|