Written in

database with Python code is very convenient, but if you do not want to write your own SQL statement, there are actually more easy way. The use of webpy's DB library is a good choice. Of course, in order to use webpy's dB, you need to install MySQLdb before, and the others don't need to do anything.

1, MySQLdb

sudo apt-get install installed base of python-MySQLdb

2, webpy

sudo apt-get install python-webpy

3 installation,

import web

db = web.database (dbn='mysql', db='blog', user='root', pw='123456')

4, to increase and change the data

 def get_pages (check): return ('pages', db.select order='id DESC') def get_page_by_url (URL): try: return db.select ('pages', where='url=$url'), vars=locals ([0]) except IndexError: return None def get_page_by_id (ID): try: return db.select ('pages', where='id=$id'), vars=locals ([0]) except IndexError: R Eturn None def new_page (URL, title, text): db.insert ('pages', url=url, title=title, content=text) def del_page (ID): db.delete ('pages', where= id=$id, vars=locals (DEF)) update_page (ID, URL, title, text): db.update ('pages', where=, vars=locals ("id=$id" url=url, title=title, content=text), 

table dB) design for

, CREATE TABLE pages (ID INT AUTO_INCREMENT, URL TEXT, Title TEXT, content TEXT, primary key (ID)); 

5,

in matters of attention to create web.database, while not connected, only set DBN, DB, user, password these basic attributes, only when select, insert, delete, update will connect.

6 and other resources

suggest that you go directly to the webpy official website to see the sample code, which is faster to learn. With regard to the operation of MySQLdb, you can see this link .

This paper fixed link:http://www.script-home.com/python-simply-implements-the-operation-of-the-mysql-database.html | Script Home | +Copy Link

Article reprint please specify:Python simply implements the operation of the Mysql database | Script Home

You may also be interested in these articles!