Django eighth Lesson 5. "Database connection, Cursor"

Source: Internet
Author: User
Tags rollback

Connection, cursor Two objects of the database 

Python Database Diagramming Process

Connection, cursor metaphor

  1. Parameter list for Connection ()

    • Host, the database server hostname of the connection, by default localhost (localhost).
    • User, the username of the connection database, default to the current user.
    • passwd, connection password, no default value.
    • DB, connected database name, no default value.
    • Conv, maps text to a Python-type dictionary.
      MySQLdb.converters.conversions
    • Cursorclass,cursor () The type used, the default value is MySQLdb.cursors.Cursor.
    • Compress, enable the Protocol compression feature.
    • Named_pipe, in Windows, is connected to a named pipe.
    • Init_command, once the connection is established, specify a statement for the database server to run.
    • Read_default_file, use the specified MySQL configuration file.
    • Read_default_group, read the default group.
    • Unix_socket, in Unix, the socket used by the connection uses TCP by default.
    • Port, which specifies the connection port for the database server, which is 3306 by default.

    • The Db.close () method of the Connection object closes the database connection and frees the related resources.

    • The Db.cursor ([Cursorclass]) method of the Connection object returns a pointer object that is used to access and manipulate the data in the database.
    • The Db.begin () method of the Connection object is used to start a transaction, and if the database's autocommit has been turned on, close it until the transaction calls commit () and rollback () end.
    • The Db.commit () and Db.rollback () methods of the Connection object represent transaction commits and fallback, respectively.
    • The Cursor.close () method of the pointer object closes the pointer and frees the related resource.
    • The Cursor.execute (Query[,parameters]) method of the pointer object executes the database query.
    • The pointer object's Cursor.fetchall () extracts all the rows in the pointer's result set, returning a tuple of result sets (tuples).
    • The Cursor.fetchmany ([size=cursor.arraysize]) of the pointer object extracts multiple rows from the query result set, and we can specify the number of rows to be fetched with optional parameters.
    • The Cursor.fetchone () of the pointer object returns the next row from the query result set.
    • The Cursor.arraysize property of the pointer object specifies the number of rows returned by the Cursor.fetchmany () method, affecting the performance of the Fetchall (), which is the default value of 1.
    • The Cursor.rowcount property of the pointer object indicates the number of rows that occurred in the last query or update. 1 indicates that the query has not started or the data has not been queried.

      Methods supported by the Connection object

  2. Cursor

    1. Common methods:

      • Close (): Close this Cursor object
      • Fetchone (): Get the next row of the result set
      • Fetchmany ([size = Cursor.arraysize]): Gets the next few rows of the result set
      • Fetchall (): Get all the rows left in the result set
      • Excute (sql[, args]): Execute a database query or command
      • Excutemany (sql, args): Execute multiple database queries or commands

    2. Common Properties:

      • Connection: Creating a database connection for this cursor object
      • ArraySize: Use the Fetchmany () method to remove the number of records at one time, default to 1

Django eighth Lesson 5. "Database connection, Cursor"

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.