--------installation
pip install pymysql
------- main method
Connect ():
the commit (): transaction commits, if not set to auto-commit, you must commit the transaction after each operation, otherwise the operation is invalid
before the operation error, you can use this function to execute a transaction rollback: rollback ()
-------------- simple example: connect to the database
1. Database query the data
SELECT book_id,book_name FROM t_book WHERE market_rule=1
2. Connect database
import pymysql import types # Connect to the database Connect = pymysql.connect (= Host " 192.168.6.41 " , User = " lrtsaudio " , password = " 2 & Ty3DW75i! (Vgo.l3Odp1fgWgEG " , Port = 3306, DB = " Audiobook " ) # using the cursor () method create a cursor object the cursor = connect.cursor () # use the cursor execute () method to perform sql statement cursor.execute ( " the SELECT book_id, book_name the FROM t_book the WHERE market_rule = 1 " ) # use fetchall () Gets all data r1 = the cursor .fetchall () Print (r1) Print (of the type (r1)) #Close the cursor connection cursor.close () # close the database connection connect.close ()
Results were as follows :( returned tuple)
You can also pass parameters through the dictionary: effect is the same
dbinfo={"host":"192.168.6.41", "user":"lrtsaudio", "password":"2&Ty3DW75i!(vgo.l3Odp1fgWgEG", "db":"audiobook" } sql="SELECT * FROM t_book WHERE market_rule=1" connect1=pymysql.connect(**dbinfo) cursor1=connect1.cursor() cursor1.execute(sql) r2=cursor1.fetchall() print(r2) cursor1.close() connect1.close
The parameters representative ------- connect () means
Host = None, # host address to connect to User = None, # database for the user's login password = '' , # password Database = None, # database to connect to Port = 0, # port, typically 3306 unix_socket is = None , # choose whether to use unix_socket rather than TCP / IP charset = '' , # character encoding sql_mode = None, # the default sql_mode to use. read_default_file = None, # read from the default configuration file (my.ini or my.cnf) in parameters taken CONV = None, # conversion dictionary = None use_unicode, # whether unicode encoding of client_flag = 0, # the Custom Send to the flags to the MySQL. constants.CLIENT in the Find potential values. cursorClass = < class ' pymysql.cursors.Cursor ' >, # select Cursor Type init_command = None, # connection established at runtime during initial statement connect_timeout the = 10, # connection time, (default: 10, min:. 1, max: 31536000) SSL = None, # a dict of arguments Similar to mysql_ssl_set () 'S parameters.For now and the cipher keyword arguments The capath are at The not Supported. read_default_group = None, #Group to the Read from in at The the Configuration File. The compress = None, # does not support named_pipe = None, # does not support no_delay = None, # autocommit = False, # whether to automatically submit the transaction db = None, # the same database, for compatibility MySQLdb passwd = none, # with the password, to be compatible MySQLdb local_infile = False, # whether to allow load local files max_allowed_packet = 16777216, # restrictions `lOCAL DATA INFILE` size defer_connect = False, # the Do not Explicitly Connect ON Contruction - the wait for Connect Call. auth_plugin_map = {},# read_timeout=None, # write_timeout=None, bind_address = None # when a customer has multiple network interfaces, specify a connection to the host
------Query data
import pymysql dbinfo={"host":"192.168.6.41", "user":"lrtsaudio", "password":"2&Ty3DW75i!(vgo.l3Odp1fgWgEG", "db":"audiobook" } sql1="SELECT book_id,book_name FROM t_book WHERE market_rule=1" sql2="SELECT * FROM audiobook.w_activity_ticket_3 WHERE user_id=234739503" connect1=pymysql.connect(**dbinfo) cursor1=connect1.cursor() cursor1.execute (SQL1) # return value is the number of rows is not affected, as follows: "" " num=cursor1.execute(sql1) print (num) # Results: num = 8 "" " R_all = cursor1.fetchall () # remove all the query results r_one cursor1.fetchone = () # fetches a row query results taken from the first row starts r_many = cursor1.fetchmany (size = 2) # remove lines wherein the query The results Print (r_all) Print (r_one) Print (r_many) cursor1.close() connect1.close()
Comment out the code fetchall result of:
! ! ! In other words:
When such fetchall (), fetchmany (), fetchone () acting simultaneously on the same query, performed at the beginning of the end of each method is a method performed