This article describes how to connect the Bottle framework of MySQL and Python. it is mainly based on the mysql-connector plug-in. if you need a lot of plug-ins, refer to the Python plug-ins for connecting mySQL, some plug-ins are specially developed under Bottle: for details about how to use bottle-mysql, refer to 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 plug-ins are, the general official driver provided by mySQL official website to Python is easy to use: the specific operations of mysql-connector 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.