"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