Python MySQL (pymsql use day1)

  1. Connect to the database
    1. check:
      1. pymysql.connect = CON (Host = "localhost", the User = "root", password = "", Database = "xxx")    # returns a value, but also need to execute a statement cursor, just like almost a relationship and threads
      2. = con.cursor the Cursor ()    # This is the cursor
      3. = sql "the SELECT * from UserInfo the WHERE the User = '% S'% (the User)"
        cursor.execute (sql)        # make the cursor to help you execute sql statement
      4. But best not to own stitching, otherwise the user may enter their special statements
        such as: Enter the user at xxx or 1 = 1 -, so adding comments will cause the judge to True
      5. So it should be = SQL "the SELECT * from UserInfo the WHERE the User =% S" 
                         cursor.excute (SQL, the User)      # excute will help our own stitching, behind the argument also supports lists and dictionaries
      6. = RET () cursor.fetchone    # is a result of performing the return value taken by fetcht, fetchtone take only one, but a plurality of times to execute the same statement taken 1,2,3 article proved when the pointer is taken in moving, so we can modify the position of the pointer
      7. cursor.scroll (. 1, MODE = 'relative')      # relative to the current position of the mobile
        cursor.scroll (2, MODE = 'Absolute')    # absolute relative position
      8. fetchmany (. 4)  # take a plurality of disposable
      9. or fetchall ()         # remove all, or take a general or all out, if taking a plurality of commands may be limited by limit
      10. When removing the original table without looking, do not know what is corresponding to each of the data, you can use the command to the data dictionary to indicate the
        cursor = con.cursor (cursor = pymysql.cursors.DictCursor)
    2. CRUD:
      1. When the additions and deletions should be added at a later cursor.execute (SQL)
        con.commit ()    # data submitted, otherwise the original table will not change
      2. If you want to add more than one time may
        cursor.executemany (SQL, [(), (), ()])      # This statement also has a return value, the number of rows affected, if the increase (modified) the first row, then the number of rows affected is 1, the same way as delete and change
      3. cursor.lastrowid ()    # can be increased from ID of new data, if you insert more than that of last increment ID
  2. Close the database
    1. con.close () 
      cursor.close ()                 # Turn off two

Guess you like

Origin www.cnblogs.com/otome/p/12483419.html