Python-MySql

Python-MySql,第1张

概述连接数据库importpymysqldb=pymysql.connect("localhost","username","password","db_name",charset="utf-8")#获取游标cursor=db.cursor()#数据库 *** 作语句.......db.close()建表sql="""createtableemploy 连接数据库
import pyMysqLdb = pyMysqL.connect("localhost","username","password","db_name",charset="utf-8")#获取游标cursor = db.cursor()#数据库 *** 作语句.......db.close()
建表
sql = """create table employee(first_name char(20) NOT NulL,last_name char(20) NOT NulL,age int,sex char(1),income float)"""cursor.execute(sql)
sql = """insert intoemployee(first_name,last_name,age,sex,income)values('hug','boy',20,'M',2001)"""try:    cursor.execute(sql)    db.commit()except:      #发生错误回滚      db.rollback()
sql = """delete from employee where age > %s" % (20)try:    cursor.execute(sql)    db.commit()except:      db.rollback()
sql = "updata employee set age = age + 2 where sex = '%c' " % ('M')try:    cursor.execute(sql)    db.commit()except:      db.rollback()
sql = """select * from employee \where income > %s" %(1000)"""try:    cursor.execute(sql)    #获取所有记录    results = cursor.fetchall()    for row in results:       fname = row[0]       lname = row[1]       age = row[2]       sex = row[3]       income = row[4]       print("fname=%s,lname=%s,age=%s,sex=%s,income=%s" %\            (fname,lname,age,sex,income))except:      print("Error: unable to feth data")
总结

以上是内存溢出为你收集整理的Python-MySql全部内容,希望文章能够帮你解决Python-MySql所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1188072.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存