python_ the mysql database connection

python connected to the remote database, to give the corresponding need to install third-party modules, an example is to mysql other empathy

1, install third-party libraries 

pip install pymysql

2, database connections, and closed

Note: Do not close the database connection, it will lead to intensive database connection pool does not release, resulting in another database connection can not connect to the database

import pymysql

# Host database service IP address 
# the User Database user name 
# password database connection password 
# db connect to the database name 
# Port database service port number 
# charset encoding 
# autocommit automatically submitted automatically submitted directly after the sql execution without again manually perform 
conn pymysql.Connect = (Host = ' Host ' , = User ' User ' , password = ' password ' , DB = ' DB ' , Port = 3306, charset = ' UTF8 ' ,
                           the autocommit = True) # database connection 
CUR = conn.cursor () # establish cursor 
cur.execute ( ' SELECT * from Table ' )   # perform sql 
# conn.commit () # sql submit the results of manual 
# cur.fetchone () # acquiring execution results single sql 
cur.fetchall () # acquires execution results of all sql 
# cur.fetchmany (10) # 10 acquires the designated execution result 
cur.close () # close the cursor 
conn.Close () # close the database

3, database queries

 

Guess you like

Origin www.cnblogs.com/xiaokuangnvhai/p/11098354.html