Python operation MySQL Database method sharing _python

Source: Internet
Author: User
Tags rollback first row create database
I am using the MySQL database for MYSQLDB operations. Let's start with a simple example:
Copy Code code as follows:

Import MySQLdb

Try
Conn=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', db= ' test ', port=3306)
Cur=conn.cursor ()
Cur.execute (' select * from user ')
Cur.close ()
Conn.close ()
Except Mysqldb.error,e:
Print "Mysql Error%d:%s"% (E.args[0], e.args[1])

Please note that you modify your database, host name, username, password.

Here's a rough example of inserting data, bulk inserting data, and updating data:
Copy Code code as follows:

Import MySQLdb

Try
Conn=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', 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 (20)) ')

value=[1, ' Hi Rollen ']
Cur.execute (' INSERT into test values (%s,%s) ', value)

Values=[]
For I in range (20):
Values.append ((i, ' Hi Rollen ' +str (i)))

Cur.executemany (' INSERT into test values (%s,%s) ', values)

Cur.execute (' Update test set info= ' I am Rollen "where id=3 ')

Conn.commit ()
Cur.close ()
Conn.close ()

Except Mysqldb.error,e:
Print "Mysql Error%d:%s"% (E.args[0], e.args[1])

Please note that you must have Conn.commit () to commit the transaction, or you cannot actually insert the data.

After running my MySQL database results are not on the map.
Copy Code code as follows:

Import MySQLdb

Try
Conn=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', port=3306)
Cur=conn.cursor ()

conn.select_db (' python ')

Count=cur.execute (' SELECT * from Test ')
print ' There has%s rows record '% count

Result=cur.fetchone ()
Print result
print ' ID:%s info%s '% result

Results=cur.fetchmany (5)
For R in results:
Print R

print ' = = ' *10
Cur.scroll (0,mode= ' absolute ')

Results=cur.fetchall ()
For R in results:
Print R[1]


Conn.commit ()
Cur.close ()
Conn.close ()

Except Mysqldb.error,e:
Print "Mysql Error%d:%s"% (E.args[0], e.args[1])

The results of the operation will not be affixed, too long.

After the query Chinese will be displayed correctly, but in the database is garbled. After I looked up from the internet, I found a property with a can be handled:

In the Python code

conn = MySQLdb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', db= ' python ') add an attribute:
To
conn = MySQLdb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', db= ' python ', charset= ' UTF8 ')
CharSet is to be the same as your database code, if the database is gb2312, then write charset= ' gb2312 '.



Here are some common functions to post:

Then, this connection object also provides support for transaction operations, standard methods
Commit () Commit
Rollback () rollback

Cursor the method used to execute the command:
Callproc (self, procname, args): Used to execute stored procedures, the received parameters are stored procedure names and parameter lists, and the return value is the number of rows affected
Execute (Self, query, args): Executes a single SQL statement, receives the parameter is the SQL statement itself and uses the parameter list, returns the value is the number of rows affected
Executemany (self, Query, args): Executes a singled out SQL statement, but repeats the arguments in the parameter list, returning the value to the number of rows affected
Nextset (self): moving to the next result set

Cursor the method used to receive the return value:
Fetchall (self): receives all returned result rows.
Fetchmany (self, Size=none): Receives the size bar to return the result row. If the value of size is greater than the number of result rows returned, the Cursor.arraysize bar data is returned.
Fetchone (self): Returns a result row.
Scroll (self, value, mode= ' relative '): Moves the pointer to a row. If mode= ' relative ', it means moving the value bar from the current row, if mode= ' absolute ', Indicates that the value bar is moved from the first row of the result set.

Resources:

MySQLdb ' s User Guide

Package MySQLdb

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.