美文网首页
在python中使用sql模块(一)

在python中使用sql模块(一)

作者: yyddpp11 | 来源:发表于2019-04-29 14:46 被阅读0次

    1.增加数据(删除、查看、改变类似操作)

    #pymysql用于python3,mysql用于python2

    import pymysql

    conn=pymysql.connect(host="localhost",db="学生信息",user="root",password="ydp19941121",charset="utf8")

    cs1=conn.cursor()

    #values里面的字符串使用单引号哦,双引号会报错!!

    mysql="insert into students(sname) values('张飞')"

    count=cs1.execute(mysql)

    #在进行增、删、改时必须使用commit()

    conn.commit()

    #查看有几行数据被修改

    print(count)

    cs1.close()

    conn.close()

    2. 多组数据插入

    1 import pymysql

    2 conn=pymysql.connect(host="localhost",db="学生信息",user="root",password="ydp19941121",charset="utf8")

     3 cs1=conn.cursor()

    4 list1=[]

    5 string1=input("请输入查询1姓名")

    6 list1.append(string1)

    7 string2=input("请输入查询2姓名")

    8 list1.append(string2)

    #不能是一个sname=%s%[a,b]这种形式,这种仅限于values可以使用:insert into students(sname) values(%s)%[a,b,c]

     9 mysql="select * from students where sname=%s or sname=%s"

    10 count=cs1.execute(mysql,list1)

    11 conn.commit()

    12 result=cs1.execute("select students.sname,scores.score,subjects.subjects from scores inner join students on stuid=students.id inner join subjects on subid=subjects. id")

    13 print(count)

    14 print(result)

    15 cs1.close()

    16 conn.close()

    相关文章

      网友评论

          本文标题:在python中使用sql模块(一)

          本文链接:https://www.haomeiwen.com/subject/kzienqtx.html