Simple Method to connect the Bottle framework of MySQL and Python, pythonbottle
Python has many connection plug-ins for mySQL, and some plug-ins are specially developed under Bottle: For details about how to use bottle-mysql, see the official website. In total, I feel that there are too many restrictions on its usage, which is inconvenient to use, of course, the most suitable is the general official driver provided by the mySQL official website to Python. It is very easy to use: mysql-connector the specific operations are as follows:
#-*-Coding: UTF-8 -*-#! /Usr/bin/python # filename: login_admin.py # codedtime: 2014-9-7 11: 26: 11 import bottleimport mysql. connector # import mysql Database connector def check_userinfo (): a_list = [] # create an empty list username = bottle. request. GET. get ('loginname ',''). strip () # Username password = bottle. request. GET. get ('Password ',''). strip () # password if username is not None or password is not None: try: # connect to database conn = mysql. connector. connect (user = 'root', pas Sword = '000000', database = 'myblog') cursor = conn. cursor () # create a data cursor # execute query = ("SELECT username, password FROM mb_users" WHERE username = % s and password = % s ") cursor.exe cute (query, (username, password) a_list = cursor. fetchall () # fetchone gets a tuples # count = int (cursor. rowcount) # Get the number of tuples return a_list using t mysql. connector. error as err: print ("Something went wrong :{}". format (err) exit () finall Y: conn. commit () # submit and modify cursor. close () # close database conn. close () else: return a_listdef login_admin (): if bottle. request. GET. get ('bs-submit ',''). strip (): # click the logon button a_list = check_userinfo () if a_list: a_name = a_list [0] [0] # Get the user name return bottle. template ('templates/index_user.tpl ', username = a_name) else: return bottle. template ('templates/login_admin.tpl ', action ='/login_admin ', error_info =' enter the correct user name or password! ') Else: return bottle. template ('templates/login_admin.tpl', action = '', error_info = '')
The above is a simple usage of MySQL in Botlle,
By the way: Install and manage mySQL. We recommend that you use XAMPP and XAMPP to integrate Apache, MySQL, PHP, Tomcat, and other tools, you do not need to install and configure them one by one, and the management is convenient. The Default User of MySQL installed in XAMPP is: the root password is blank.