300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Python MySQL进行数据库表变更和查询

Python MySQL进行数据库表变更和查询

时间:2021-06-11 11:04:47

相关推荐

Python MySQL进行数据库表变更和查询

数据库|mysql教程

Python,进行数据库表变更和查询,Python,MySQL的基础操作

数据库-mysql教程Python连接MySQL,进行数据库表变更和查询:

易语言仿 源码,vscode 保存临时文件,ubuntu火狐花屏,tomcat无法关联项目,头条爬虫技术,php例子大全,南沙区创新seo优化有哪些lzw

python mysql insert delete query:

政府cms源码,ubuntu取消开机自检,python爬虫考试视频,php下页,seo基础介绍lzw

选择题考试软件源码,Ubuntu桌面暗,tomcat网页不显示内容,怎么网页爬虫,php环境模拟器,青州seo关键词优化费用lzw

#!/usr/bin/python import MySQLdb def doInsert(cursor,db): #insert # Prepare SQL query to INSERT a record into the database. sql = "UPDATE EMPLOYEE SET AGE = AGE+1 WHERE SEX = \%c\" %(M) try:cursor.execute(sql)mit() except:db.rollback() def do_query(cursor,db): sql = "SELECT * FROM EMPLOYEE \WHERE INCOME > \%d\" % (1000) try:# Execute the SQL commandcursor.execute(sql)# Fetch all the rows in a list of lists.results = cursor.fetchall()print esuts,cursor.rowcountfor 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" def do_delete(cursor,db): sql = DELETE FROM EMPLOYEE WHERE AGE > {}.format(20) try:cursor.execute(sql)mit() except:db.rollback() def do_insert(cursor,db,firstname,lastname,age,sex,income): sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \LAST_NAME, AGE, SEX, INCOME) \VALUES (\%s, \%s, \%d, \%c, \%d )" % \(firstname,lastname,age,sex,income) try:cursor.execute(sql)mit() except:db.rollback() # Open database connection # change this to your mysql account #connect(server,username,password,db_name) db = MySQLdb.connect("localhost","root","root","pydb" ) # prepare a cursor object using cursor() method cursor = db.cursor() do_query(cursor,db) doInsert(cursor,db) do_query(cursor,db) do_delete(cursor,db) do_query(cursor,db) do_insert(cursor,db,hunter,xue,22,M,2000) do_insert(cursor,db,mary,yang,22,f,5555) do_insert(cursor,db,zhang,xue,32,M,5000) do_insert(cursor,db,hunter,xue,22,M,333) do_query(cursor,db) # disconnect from server db.close()

之后可以在此基础上根据需要进行封装。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。