python3.5 连接mysql

 

I did the steps below with Python 3.5.1 and it works:

  • Download driver from here
  • Driver installation in cmd, in this folder PythonPython35PyMySQL-0.7.4pymysql

    python setup.py build
    python setup.py install
  • Copy folder PythonPython35PyMySQL-0.7.4pymysql to PythonPython35pymysql

  • Sample code in python IDE

    import pymysql
    import pymysql.cursor
    conn= pymysql.connect(host='localhost',user='user',password='user',db='testdb',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
    a=conn.cursor()
    sql='CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT,`email` varchar(255) NOT NULL,`password` varchar(255) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'
    a.execute(sql)
  • Enjoy It!
原文地址:https://www.cnblogs.com/to-creat/p/6489499.html