#MySQL for Python (MYSQLDB) Note

Source: Internet
Author: User

#MySQL for Python (MYSQLDB) Note#remember not to create a table in Python, just make additions and deletions. #steps: (0) Reference Library--(1) Create a connection--(2) Create a cursor--(3) Select Database--(4) EXECUTE Statement--(5) Close the connection#(0) Reference Library Import  mysqldb #(1) Create a connectionCon =MySQLdb.connect(user ="Root", passwd ="123456", host ="127.0.0.1")#(2) Creating CursorsCur =con. cursor ()#(3) Select DatabaseCon.select_db("Ivms8100v3")#(4) EXECUTE statement#(4.1) Single-line insertionSqli ="INSERT into Users (StrName, Strcode, nlevel) value (%s,%s,%d)"cur. Execute (Sqli, ("Admin","Admin", 3))#(4.2) Multi-row insertionSqlim ="INSERT into Users (StrName, Strcode, nlevel) values (%s,%s,%d)"cur. Executemany (Sqlim, [("Xjh","Admin", 3), ("XJH2","Admin", 3)])#(4.3.1) Single queryCur.Execute("SELECT * from Users")#before calling Fetchone (), scroll (), Fetchmany (), query firstCur.Fetchone()#The default starts with the first record, and the pointer points to the next record. #(4.3.2) specifying a queryCur.Scroll(0,"Absolute")#absolute position indexed to 1 in the track record#(4.3.3) multi-line queryCur.Fetchmany(15)#Specifies to view 15 entries, pointing to the next record. #(4.3.4) multi-line query completed in one stepCur.Fetchmany(cur.Execute("SELECT * from Users")) #(5) Close the connectionCur.Close()#close the cursor firstCon.Close()#Close the connection again

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.