Connect to MySQL

Source: Internet
Author: User

Install: File---setting--project---project interpreter--Select version, click on the right + sign, search pymysql---Install

Steps:

1, connect to MySQL, specify IP port number, account, password, database, equivalent to open the warehouse door

2, create a cursor, the cursor equivalent to the warehouse administrator, help find things

3, execute SQL, use Execute

4. Get Results

Import Pymysql

Conn=pymysql.connect (host= ' 211.149.218.16 ', user= ' jxz ', passwd= ' 123456 ',

port=3306,db= ' Jxz ', charset= ' UTF8 ') charset specifies the character set type and must be written UTF8

Port does not exist can not write, but write must be int type

Yb=conn.cursor () warehouse Manager

Yb.execute (' select * from Bt_stu limit 5; ')  Execute SQL statement SQL statement with "; The end of the number, or it will be error

Res=yb.fetchall () gets the return result

Print (RES)

Yb.close () Close cursor

Conn.close () Close link

Run results (two-dimensional tuples (tuples can also be used with the value of a corner label)):

(502, ' Chou Heung ', 1, ' 18612341231 ', ' Patron tun ', 1), (503, ' Wang Lan ', 1, ' 18723561789 ', ' Scorpio 3 ', 1 '), (506, ' Xiu Xian ', 1, ' 18688866686 ', ' Road cut ', 1 ), (508, ' Jia Meng ', 1, ' 18612333231 ', ' Patron tun ', 1), (511, ' Hermes ', 1, ' 18332341231 ', ' Patron tun ', 1))

======================================================================================

1) cursors. Fetchon () Run results take one piece of data, and the data is placed into a one-dimensional tuple for the scenario: to determine that the return result is only one piece of data

2) Fetchone and Fetchiall are used simultaneously:

Fetchone before, fetchall after, Fetchall read the data after the Fetchone read;

Fetchall before, fetchone after, Fetchall read all data, Fetchone read the data is empty, return none

3) Move the file pointer

cursor. Scroll (0,mode= ' absolute ') moves the pointer to the front, adsolute the absolute position, that is, the front

cursor. Scroll (0,mode= ' relative ') move the pointer to the front, relative relative to the current position, if the 0 is modified to 1, then the value is the second-to-last value

==================================================================================

When the INSERT Update Delect statement executes, the commit action needs to be executed after the SQL statement is executed: Database connection name. Commit ()

Import Pymysql

Conn=pymysql.connect (host= ' 211.149.218.16 ', user= ' jxz ', passwd= ' 123456 ',

port=3306,db= ' Jxz ', charset= ' UTF8 ') charset specifies the character set type and must be written UTF8

Port does not exist can not write, but write must be int type

Yb=conn.cursor ()

Sql= "INSERT into ' bt_stu ' (' real_name ', ' sex ', ' phone ', ' class ', ' type ') VALUES (' Ahab ', ' 1 ', ' 17411348971 ', ' sss ', ' 1 '); "

Yb.execute (SQL)

Conn.commit () Submit action

Print (Yb.fetchone) Run Result: none

==========================================================================================

Conn=pymysql.connect (host= ' 211.149.218.16 ',
User= ' Jxz ', passwd= ' 123456 ',
port=3306,db= ' Jxz ', charset= ' UTF8 ') #必须写utf8
cur = conn.cursor(cursor=pymysql.cursors.dictcursor) converts tuples to dictionaries or lists (as determined by Fetchone or fetchall)
Sql= ' select * from Bt_stu limit 5 '
Cur.execute (SQL)
Print (Cur.fetchone ())
Print (Cur.fetchall ())
Operation Result:

Dictionary:

{' Sex ': 1, ' class ': ' Patron tun ', ' type ': 1, ' ID ': 502, ' real_name ': ' Chou-Heung ', ' phone ': ' 18612341231 '}
List

[{' Sex ': 1, ' class ': ' Scorpio 3 ', ' type ': 1, ' ID ': 503, ' real_name ': ' Wang Lan ', ' phone ': ' 18723561789 '}, {' Sex ': 1, ' class ': ' Cut the road ', ' Type ': 1, ' ID ': 506, ' real_name ': ' Fix the fairy ', ' phone ': ' 18688866686 '}, {' Sex ': 1, ' class ': ' Patron tun ', ' type ': 1, ' ID ': 508, ' Real_n Ame ': ' Jia Meng margin ', ' phone ': ' 18612333231 '}, {' Sex ': 1, ' class ': ' Patron tun ', ' type ': 1, ' id ': ' 511 ', ' real_name ': ' Ai-da ', ' phone ': ' 18332 341231 '}]

Connect to MySQL

Related Article

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.