Home Database Mysql Tutorial web.py 数据库操作指南

web.py 数据库操作指南

Jun 07, 2016 pm 03:23 PM
h web.py Operation guide database

原文:http://chenxiaoyu.org/blog/archives/226 官网地址:http://webpy.org/ web.py是一个小巧灵活的框架,最新稳定版是0.33。这里不介绍web开发部分,介绍下关于数据库的相关操作。 很多Pyer一开始都喜欢自己封装数据库操作类,本人亦如此。不过后来通过

原文:http://chenxiaoyu.org/blog/archives/226

 

官网地址:http://webpy.org/

web.py是一个小巧灵活的框架,最新稳定版是0.33。这里不介绍web开发部分,介绍下关于数据库的相关操作。

很多Pyer一开始都喜欢自己封装数据库操作类,本人亦如此。不过后来通过观摩web.py的源码,发现其数据库操作部分相当紧凑实用。推荐懒人可以尝试一下。

废话不多,先来安装,有两种方式:

1. easy_install方式,如果木有此工具,可以参考:http://chenxiaoyu.org/blog/archives/23

1

easy_install web.py

Copy after login

2. 下载源码编译。地址: http://webpy.org/static/web.py-0.33.tar.gz ,解压后执行:

1

python setup.py install

Copy after login

web.py安装算到此结束,如果想使用其中的db功能,还得借助与相应数据库操作模块,比如MySQLdb、psycopg2。如果需要尝试连接池(database pool)功能,还得装下DBUtils。这几个模块都可以通过easy_install来安装。

下面开始使用吧!

1. 导入模块,定义数据库连接db。

1

2

import web

db = web.database(dbn='postgres', db='mydata', user='dbuser', pw='')

Copy after login

2. select 查询

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

# 查询表

entries = db.select('mytable')

 

# where 条件

myvar = dict(name="Bob")

results = db.select('mytable', myvar, where="name = $name")

results = db.select('mytable', where="id>100")

 

# 查询具体列

results = db.select('mytable', what="id,name")

 

# order by

results = db.select('mytable', order="post_date DESC")

 

# group

results = db.select('mytable', group="color")

 

# limit

results = db.select('mytable', limit=10)

 

# offset

results = db.select('mytable', offset=10)

Copy after login

3. 更新

1

db.update('mytable', where="id = 10", value1 = "foo")

Copy after login

4. 删除

1

db.delete('mytable', where="id=10")

Copy after login

5. 复杂查询

1

2

3

4

5

6

7

8

9

# count

results = db.query("SELECT COUNT(*) AS total_users FROM users")

print results[0].total_users

 

# join

results = db.query("SELECT * FROM entries JOIN users WHERE entries.author_id = users.id")

 

# 防止SQL注入可以这么干

results = db.query("SELECT * FROM users WHERE id=$id", vars={'id':10})

Copy after login

6 多数据库操作 (web.py大于0.3)

1

2

3

4

5

db1 = web.database(dbn='mysql', db='dbname1', user='foo')

db2 = web.database(dbn='mysql', db='dbname2', user='foo')

 

print db1.select('foo', where='id=1')

print db2.select('bar', where='id=5')

Copy after login

7. 事务

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

t = db.transaction()

try:

    db.insert('person', name='foo')

    db.insert('person', name='bar')

except:

    t.rollback()

    raise

else:

    t.commit()

 

# Python 2.5+ 可以用with

from __future__ import with_statement

with db.transaction():

    db.insert('person', name='foo')

    db.insert('person', name='bar')

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

How does Go language implement the addition, deletion, modification and query operations of the database?

OPPO11 lock screen gesture operation guide OPPO11 lock screen gesture operation guide Mar 25, 2024 am 08:36 AM

OPPO11 lock screen gesture operation guide

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

How does Hibernate implement polymorphic mapping?

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

An in-depth analysis of how HTML reads the database

Analysis of the basic principles of MySQL database management system Analysis of the basic principles of MySQL database management system Mar 25, 2024 pm 12:42 PM

Analysis of the basic principles of MySQL database management system

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How does Go WebSocket integrate with databases?

See all articles