Use pymysql to connect to the MySql database and pymysqlmysql Database

Source: Internet
Author: User

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 ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.