Python access to the MySQL database can use the MYSQLDB module, the main method is as follows:
(1) Commit (): commits the transaction.
(2) Rollback (): ROLLBACK TRANSACTION.
(3) Callproc (Self,procname,args): Used to execute stored procedures, accepted parameters are stored procedure name and parameter list, inverse value is the number of rows affected.
(4) Execute (self,query,ages): Executes a single SQL statement, the receive side parameter is the SQL statement itself and the parameter list used, the return value is the number of rows affected.
(5) Executemany (Self,query,args): Executes a single SQL statement, but repeats the parameters in the list of parameters, and the return value is the affected function.
(6) Nextset (self): moves to the next result set.
(7) Fetchall (self): receives all the returned result rows.
(8) Fetchmany (Self,size=none): Receives a size bar return result, if the value of size is greater than the number of returned result rows, the Cursor.arraysize data is returned.
(9) Fetchone (self): Returns a result row.
(ten) Scroll (self,value,mode= ' relative '): Moves the pointer to a line, if mode= ' relative ', indicates that the value record is moved from the current row, if mode= ' Absoulte ', Indicates that the value record is moved from the first row of the result set.
1 ImportMySQLdb2 3 #querying data from the MySQL database4 5 Try:6conn = MySQLdb.connect (host='localhost', user='Root', passwd='Root', db='Test', port=3306)7Cur =conn.cursor ()8Cur.execute ('SELECT * from user')9 cur.close ()Ten conn.close () One exceptMysqldb.error as E: A Print('MySQL Error%d:%s'% (e.args[0],e.args[1])) - - the #inserting data into the MySQL database - Try: -conn = MySQLdb.connect (host='localhost', user='Root', passwd='Root', db='Test', port=3306) -Cur =conn.cursor () +Cur.execute ('CREATE database if not exists Python') -conn.select_db ('python') +Cur.execute ('CREATE TABLE Test (ID int,info varchar )') AValue=[1,'Hi Rollen'] atCur.execute ('INSERT into test values (%s,%s)', value) -Values = [] - forIinchRange (20): -Values.append ((I,'Hi Rollen'+str (i))) - -Cur.executemany ('INSERT into test values (%s,%s)', values) inCur.execute ('Update Test Set info = "I am Rollen" WHERE id = 3') - Conn.commit () to cur.close () + conn.close () - exceptMysqldb.error as E: the Print('MySQL Error%d:%s'% (E.args[0], e.args[1]))
8.2.3 working with MySQL database