# -*- 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 密碼為空。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com