Use pymysql to connect to the MySql database and pymysqlmysql Database
MySQLdb installation fails. You can directly use pymysql to install pymysql.
I learned how to use pymysql to create databases and tables and insert data.
_ Author _ = 'admin' import pymysqltry: conn = pymysql. connect (host = 'localhost', port = 3306, user = 'root', passwd = '', charset = 'utf8') # conn = pymysql. connect (host = 'localhost', user = 'root', passwd = '', db = 'bookdb') cur = conn. cursor () # obtain a cursor object cur.exe cute ("create database test15") # execute the corresponding SQL statement cur.exe cute ("USE test15 ") # create database cur.exe cute ("create table users (id INT, name VARCHAR (18)") # create table cur.exe cute ("insert into users VALUES (1, 'blog '), (2, 'csdn'), (3, 'net'), (4, 'a359680405 ')") # insert data cur.exe cute ("SELECT * FROM users") data = cur. fetchall () for row in data: print ('% s \ t % s' % row) cur. close () # close the cursor conn. commit () # submit any outstanding transactions to the database, and do not perform any operations on the database that does not support transactions. close () # close the connection to the database and release the database resource snapshot t Exception: print ("Exception occurred ")