Implement mysql database using Python

Source: Internet
Author: User
Mysql database operations implemented by Python, query, delete, insert, update, and so on for specified tables in the database. Remember dbweb. database (dbnmysql, dbpy, userroot, pw123456 ). 1. Simple #-*-coding: UTF-8-*-@ author: cuiruiqiang @ date: 2014-12-05 @ filename: testDatabas

Mysql database operations implemented by Python, query, delete, insert, update, and so on for specified tables in the database. Remember that db = web. database (dbn = 'mysql', db = 'py', user = 'root', pw = '123 '). 1. Simple #-*-coding: UTF-8-*-''' @ author: cuiruiqiang @ date: 2014-12-05 @ filename: testDatabas

Mysql database operations implemented by Python, query, delete, insert, update, and so on for specified tables in the database. Remember that db = web. database (dbn = 'mysql', db = 'py', user = 'root', pw = '123 ').

1. Simple

#-*-Coding: UTF-8 -*-
'''
@ Author: cuiruiqiang
@ Date: 2014-5 5
@ Filename: testDatabase. py
'''
Import web
Import sys
# Define a database object defines a database object
Db = web. database (dbn = 'mysql', db = 'py', user = 'root', pw = '123 ')
# Define a table defines a table
Tb = 'student'
Def get_by_id (id ):
S = db. select (tb, where = 'id = $ id', vars = locals ())
If not s:
Return False
Return s [0]
If _ name _ = '_ main __':
ResultValue = get_by_id (3)
Print resultValue
Print db

2. Complicated. mysql operating languages such as select, update, insert, and delete are introduced.


#-*-Coding: UTF-8 -*-
'''
@ Author: cuiruiqiang
@ Date: 2014-12-07
@ Filename: testDatabase2.py
'''
Import web
Import sys
# Define a database object
Db = web. database (dbn = 'mysql', db = 'py', user = 'root', pw = '123 ')
# Define a table
Tb = 'student'
Def getTableInfo (id ):
Result = db. query ('select * from student where id> = $ id', vars = locals ())
For s in result:
Print s. id, s. name, s. age, s. address
If _ name _ = '_ main __':
# Simple select
Result = db. query ('select * from student where id> $ id', vars = {'id': 1 })
For s in result:
Print s. id, s. name, s. age, s. address
# Insert
Db. insert ('student ', id = 4, name = 'lijliang', age = 32, address = 'hebei baoj ')
GetTableInfo (2)
# Update
Db. update ('student ', where = 'id = $ id', vars = {'id': 2}, name = 'xiaom ')
GetTableInfo (2)
# Delete
Db. delete ('student ', where = 'id = $ id', vars = {'id': 1 })
GetTableInfo (2)

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.