8.2.3 working with MySQL database

Source: Internet
Author: User

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

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.