Python访问Mysql

import pymysql
coon = pymysql.connect(
      host = '127.0.0.1',
      user = 'root',
      passwd = '123456',
      port = 3306, #port必须写int类型
      db = 'mydb',
      charset = 'utf8' #charset必须写utf8,不能写utf-8
    )
cur = coon.cursor()  #建立游标
cur.execute('delete from student where id=5')
cur.execute('insert into student (id, name) values (5,\'张三\')')
coon.commit()
cur.execute("select * from student")
res = cur.fetchall()    #获取结果
print(res)
cur.close()     #关闭游标
coon.close()    #关闭连接

注意,请先PIP 下 pymysql

 

Enjoy :)

 

posted @ 2019-08-15 13:54  bigdog  阅读(169)  评论(0编辑  收藏  举报