bottle mysql_Bottle实例之-MySQL连接

Python关于mySQL的连接插件众多,Bottle下也有人专门开发的插件:bottle-mysql具体使用方法见官方,总共感觉其用法限制太多,其使用起来不方便,最适合的当然是,mySQL官网给Python提供的通用官方驱动,用起来很顺手:mysql-connector  具体操作如下:

Python Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

# -*- 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  密码为空,如果想修改用户、密码等:谷歌:"Xampp 修改mySQL密码";这里提供一篇通俗易懂的方法见:

再说一下安装mysql-connector:为了方便其他人下载安装,我把该资源进行了上传。

也可以去mySQL官网下载安装msi安装程序:http://dev.mysql.com/downloads/connector/   不过要于版本于Python的版本对应,不然安装不了!

mysql-connector模块的安装同其他Python模块安装方法相同,最简便的方法就是用easy_install安装:执行:easy_install  mysql-connector即可自动完成相应模块的寻找安装,前天是先安装easy_install;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值