Python connection MySQL Database example (do additions and deletions, change operation) _python

Source: Internet
Author: User

First, related code
Database Configuration Class mysqldbconn.py

Copy Code code as follows:

#encoding =utf-8
'''
Created on 2012-11-12

Mysql Conn Connection Class
'''

Import MySQLdb

Class Dbconn:

conn = None

#建立和数据库系统的连接
def connect (self):
Self.conn = MySQLdb.connect (host= "localhost", port=3306,user= "house", passwd= "house", db= "house", charset= "UTF8")

#获取操作游标
def cursor (self):
Try
Return Self.conn.cursor ()
Except (Attributeerror, Mysqldb.operationalerror):
Self.connect ()
Return Self.conn.cursor ()

def commit (self):
Return Self.conn.commit ()

#关闭连接
def close (self):
Return Self.conn.close ()

Mysqldemo.py class

Copy Code code as follows:

#encoding =utf-8
'''
Created on 2012-11-12

@author: Steven

MySQL Operation demo
Done: CREATE TABLE, delete table, data add, delete, change, BULK INSERT
'''
Import Mysqldbconn

Dbconn = Mysqldbconn.dbconn ()

def process ():
#建立连接
Dbconn.connect ()
#删除表
Droptable ()
#创建表
CreateTable ()
#批量插入数据
Insertdatas ()
#单条插入
InsertData ()
#更新数据
UpdateData ()
#删除数据
DeleteData ()
#查询数据
Querydata ()
#释放连接
Dbconn.close ()

Def insertdatas ():
sql = "INSERT into Lifeba_users (name, realname, age) values (%s,%s,%s)"
TMP = (' steven1 ', ' Test 1 ', '), (' Steven2 ', ' Test 2 ', 25)
Executemany (SQL, TMP)

Def updatedata ():
sql = "Update lifeba_users set realname = '%s ' WHERE name = ' steven1 '"% ("Test 1 Modified")
Execute (SQL)

Def deletedata ():
sql = "Delete from lifeba_users where id=2"
Execute (SQL)

Def querydata ():
sql = "SELECT * FROM Lifeba_users"
rows = query (SQL)
Printresult (rows)

Def insertdata ():
sql = "INSERT into Lifeba_users (name, realname, age) VALUES ('%s ', '%s ',%s ')"% ("Steven3", "Test 3", "26")
Print SQL
Execute (SQL)

def executemany (SQL, TMP):
"Insert more than one data"
Conn=dbconn.cursor ()
Conn.executemany (SQL, TMP)

def execute (SQL):
' Execute SQL '
Conn=dbconn.cursor ()
Conn.execute (SQL)

def query (SQL):
"Query SQL" "
Conn=dbconn.cursor ()
Conn.execute (SQL)
rows = Conn.fetchmany (10)
return rows

Def createtable ():
' Create a table '
Conn=dbconn.cursor ()
Conn.execute (""
CREATE TABLE ' Lifeba_users ' (
' ID ' int (one) not NULL auto_increment,
' name ' varchar default NULL,
' realname ' varchar default NULL,
' Age ' int (one) default NULL,
PRIMARY KEY (' ID ')
) Engine=myisam DEFAULT Charset=utf8;
''')
# Dbconn.commit ()

Def droptable ():
"Delete Table" "
Conn=dbconn.cursor ()
Conn.execute (""
DROP TABLE IF EXISTS ' lifeba_users '
''')
# Dbconn.commit ()

def printresult (rows):
For row in rows:
For I in range (0,len (row)): #遍历数组
Print Row[i], #加, do not wrap lines
print '

if __name__ = = ' __main__ ':

Process ()

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.