python连接mysql数据库操作

import mysqldb # open database connection db = mysqldb.connect(“localhost”,”testuser”,”test123″,”testdb” ) # prepare a cursor object using cursor() method cursor = db.cursor() # prepare sql query to insert a record into the database. sql = “insert into employee(first_name, \ last_name, age, sex, income) \ values (‘%s’, ‘%s’, ‘%d’, ‘%c’, ‘%d’ )” % \ (‘mac’, ‘mohan’, 20, ‘m’, 2000) try: # execute the sql command cursor.execute(sql) # commit your changes in the database db.commit() except: # rollback in case there is any error db.rollback() # disconnect from server db.close() 读取操作: fetchone(): 这种方法获取查询结果集的下一行。结果集是一个对象时,将返回一个游标对象用于查询表. fetchall(): 这种方法获取结果集的所有行。如果某些行已经从结果集中提取,fetchall()方法检索结果集的其余行. rowcount: 这是一个只读属性,返回受影响的行数execute()方法. #!/usr/bin/python import mysqldb # open database connection db = mysqldb.connect(“localhost”,”testuser”,”test123″,”testdb” ) # prepare a cursor object using cursor() method cursor = db.cursor() # prepare sql query to insert a record into the database. sql = “select * from employee \ where income > ‘%d'” % (1000) try: # execute the sql command cursor.execute(sql) # fetch all the rows in a list of lists. results = cursor.fetchall() for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # now print fetched result print “fname=%s,lname=%s,age=%d,sex=%s,income=%d” % \ (fname, lname, age, sex, income ) except: print “error: unable to fecth data” # disconnect from server db.close() 更新操作: 对任何数据库更新操作意味着更新已经可以在数据库中的一个或多个记录。以下是更新所有的记录为“m”sex的过程。在这里,我们将所有男性年龄增加一年. #!/usr/bin/python import mysqldb # open database connection db = mysqldb.connect(“localhost”,”testuser”,”test123″,”testdb” ) # prepare a cursor object using cursor() method cursor = db.cursor() # prepare sql query to update required records sql = “update employee set age = age + 1 where sex = ‘%c'” % (‘m’) try: # execute the sql command cursor.execute(sql) # commit your changes in the database db.commit() except: # rollback in case there is any error db.rollback() # disconnect from server db.close() 删除操作: delete操作是必需的,当你想从数据库中删除一些记录。以下是程序删除雇员的所有记录,其中年龄超过20岁. 例子: #!/usr/bin/python import mysqldb # open database connection db = mysqldb.connect(“localhost”,”testuser”,”test123″,”testdb” ) # prepare a cursor object using cursor() method cursor = db.cursor() # prepare sql query to delete required records sql = “delete from employee where age > ‘%d'” % (20) try: # execute the sql command cursor.execute(sql) # commit your changes in the database db.commit() except: # rollback in case there is any error db.rollback() # disconnect from server db.close() 执行事务: 事务是机制,以确保数据的一致性,事务应该有以下四个属性: 原子性: 无论是事务结束或什么也没有发生在所有. 一致性: 必须启动一个事务一致的状态和离开系统是一致的状态. 隔离性: 在当前事务外,事务的中间结果是不可见的. 持久性: 一旦事务提交,效果是持久的,即使系统发生故障后. 对python db api 2.0提供两种方法来提交或回滚事务. ——————————————————————– import mysqldb con = mysqldb.connect(host=’localhost’, user=’root’, passwd=’root’, db=’hr_resume_center’, charset=’utf8′) cursor = con.cursor() sql = “insert into hr_resume_core (resume_id,name,mobile,email,add_time,sys_time,version) values(%s,%s,%s,%s,%s,%s,%s)” param = [ (1,’bright’,’13641154657′,’jackieming\@sina.cn’,1385625423,1385625423,1), (2,’bright’,’13641154657′,’jackieming\@sina.cn’,1385625423,1385625423,1), (3,’bright’,’13641154657′,’jackieming\@sina.cn’,1385625423,1385625423,1), ] cursor.execute(sql,param) cursor.close() con.close()

Posted in 未分类

发表评论