Webpy using MySQL database operations (web.database)

Source: Internet
Author: User

Webpy_web.database Module

There are two ways to use the MySQL management database in the WEBPY framework, one is to use the MySQLdb module in Python:

Import MySQLdb

There is also a web.database module with Webpy, the function is basically the same as the MySQLdb module:

Import Web db = Web.database (        dbn = ' mysql ',        user = ' root ',        pw = ' password ',        db = ' db_name ',        )

Above is the creation of a database object db, the parameter user is the username, PW is the password, DB is the database name

The DB object supports the following operations:

    • Insert
    • Select
    • Update
    • Delete
    • Multiple inserts
    • Advanced querying
    • Joining tables
Inserting

Build a table first Todos

CREATE TABLE users (        ID int primary key auto_increment,        name nchar,        password nchar),        address NCHAR (20))

  

Insert an item of data:

Db.insert (' users ', name = ' Bob ', password = ' 123 ', address = ' Zhuhai ')

  

selecting

Select returns an object of type ' Web.iterbetter ', which can be converted to list () for processing, the element of each list is the storage type, similar to the Python dictionary, can be directly through the keyword to get the desired value

Users = List (Db.select (' Users ', where= "id>10")) print users[0][' name '] ' Bob '
Updating
num_updated = Db.update (' users ', where= "id = ten", address = ' Guangzhou ')

The return value is the modified number of rows

deleting

Db.delete (), as with update () usage

Advanced querying

If you are familiar with MySQL statements, you can directly execute the MySQL statement directly:

Results = List (Db.query ("SELECT * from users where name = '%s '"% ' Bob ')) print results[0][' name '], results[0][' address '] ' Bob ', ' Zhuhai '

Webpy using MySQL database operations (web.database)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.