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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)