Delete Data
Delete A single piece of data
#conding=utf-8import MySQLdbimport randomtry: conn = MySQLdb.connect( host = ‘127.0.0.1‘, user = ‘root‘, passwd = "123456", port = 3306) conn.select_db(‘python‘)# 选择pythonDB数据库 cur = conn.cursor()# 获取游标 cur.execute("delete from user where id=0") cur.execute("select * from user") for v in cur.fetchall(): print v cur.close() conn.commit() conn.close() print u"sql执行成功"except Exception, e:print e
Delete more than one piece of data
#conding=utf-8import MySQLdbimport randomtry: conn = MySQLdb.connect( host = ‘127.0.0.1‘, user = ‘root‘, passwd = "123456", port = 3306) conn.select_db(‘python‘)# 选择pythonDB数据库 cur = conn.cursor()# 获取游标 sql = "delete from user where id=%s" cur.executemany(sql,[(3),(2,)]) cur.execute("select * from user") for v in cur.fetchall(): print v cur.close() conn.commit() conn.close() print u"sql执行成功"except Exception, e: print e
Transaction rollback
import MySQLdbimport randomtry: conn = MySQLdb.connect( host = ‘127.0.0.1‘, user = ‘root‘, passwd = "123456", port = 3306) conn.select_db(‘python‘)# cur = conn.cursor()# 获取游标 cur.execute("select * from user") datas = cur.fetchall() print u"修改前的数据:\n", datas[0] cur.execute("update user set birthday=‘2100-08-12‘where name=‘tom6") cur.execute("select * from user") datas = cur.fetchall() print u"修改后的数据:\n", datas[0] #回滚事务 conn.rollback() cur.execute("select * from user") datas = cur.fetchall() print u"事务回滚后的数据:\n", datas[0] #关闭游标 cur.close() #提交事务 conn.commit() #关闭数据库连接 conn.close() print u"sql语句执行成功!"except Exception, e: print e
Reset Cursor Position
Scroll (value, mode= ' relative ')
Moves the pointer to the line specified by the parameter value;
Mode = relative indicates that the value row is moved forward from the current row
The Mode=absolute represents the value row that is moved to the absolute position. Cursor Index starting from 0
Cursor.rownumber
Returns the position of the current cursor
database table Contents:
#coding =utf-8import mysqldb# Open the database connection conn = MySQLdb.connect (host = "localhost", port = 3306, user = "R Oot ", passwd =" 123456 ", db =" Python ", CharSet =" UTF8 ") #使用cursor () method gets the operation cursors of the database cursor = conn.cursor () cursor. Execute ("SELECT * from the user order by id") Print u "Current cursor location:", Cursor.rownumberprint cursor.fetchone () print U "Current cursor location:", C Ursor.rownumbercursor.scroll (0,mode= "Absolute") #移动游标到第一行print U "Current cursor location:", Cursor.rownumberprint Cursor.fetchone ()
#coding=utf-8import MySQLdb#打开数据库连接conn = MySQLdb.connect( host = "localhost", port = 3306, user = "root", passwd = "123456", db = "python", charset = "utf8") #使用cursor()方法获取数据库的操作游标cursor = conn.cursor()cursor.execute("select * from user order by id")print u"当前游标所在位置:", cursor.rownumberres = cursor.fetchmany(5)for value in res: print valueprint u"当前游标所在位置:", cursor.rownumbercursor.scroll(3,mode="relative")#游标会向前移动3,对应数据库表就是向后移动3行print u"当前游标所在位置:", cursor.rownumberprint cursor.fetchone()
MYSQLDB Operational Database (iii)