python通过MySQLdb模块连接查询mysql数据,mysqldbmysql,#!/usr/bin/p
文章由Byrx.net分享于2019-03-23 08:03:08
python通过MySQLdb模块连接查询mysql数据,mysqldbmysql,#!/usr/bin/p
#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = 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 serverdb.close()
输出结果
fname=Mac, lname=Mohan, age=20, sex=M, income=2000
相关内容
- python超简代码解决约瑟夫环问题,python约瑟夫,a = [ x
- python 日志分析,python,分析日志,取出访问最多的
- 装饰器三个例子,装饰三个例子,import loggi
- 数据结构——链表,数据结构,#utf-8import
- 专用链接解码,链接解码,利用PyQt做界面,将迅
- nginx下使用webpy框架简单小例子,nginxwebpy,nginx下使用w
- python PIL合并半透明的png图片,,如下代码片段使用pyth
- 来个一句话生成随机大乐透,一句话生成,import rando
- python 替换文本,,'''Created o
- python中的日期和时间格式化输出,python日期,下面是格式
评论关闭