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 下 pymysqlmysql

 

Enjoy :)sql

相關文章
相關標籤/搜索