Python interview with MySQL-Rookie notes

Source: Internet
Author: User

"Basic Ideas"

Access the remote MySQL database and do DDL and DML operations with local python scripts

1 #-*-encoding:utf-8-*-2 """3 test python to MySQL connection and operation4 """5 6 ImportMySQLdb7 8Connstr="""host= ' 134.64.14.48 ', port=3306,user= ' root ', passwd= ' ******* ', db= ' test '"""9 #Connection StringTen  Oneconn=MySQLdb.connect (CONNSTR) A #Create a link -Mycursor=conn.cursor () - #get cursor, manipulate database with cursor the  - #Create a table -ctable="""CREATE TABLE Test (name VARCHAR), ID INT (TEN) primary key)""" - Mycursor.execute (ctable) +  - #Inserting Data +rec1="""INSERT into Test values (' A1 ', one-by-one)""" ARec2="""INSERT into Test values (' A2 ',""" atrec3="""INSERT into Test values (' A3 ', +)""" -recs=[] - recs.append (REC1) - recs.append (REC2) - recs.append (REC3) -  forInsertinchRECs: in      Try: - mycursor.execute (insert) to Conn.commit () +      exceptexception,e: -             Printe the Conn.rollback () *              $Conn.close () #关闭链接

MyCursor the method used to execute the command:
Callproc (self, procname, args): Used to execute stored procedure, received parameter is stored procedure name and parameter list, return value is the number of rows affected
Execute (Self, query, args): Executes a single SQL statement, receives the parameters for the SQL statement itself and the parameter list used, and returns the number of rows affected
Executemany (self, Query, args): Executes a single SQL statement, but repeats the parameters in the list of parameters, with the returned value being the number of rows affected
Nextset (self): move to the next result set

Python interview with MySQL-Rookie notes

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.