Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Database Systems - Python Database Programming - Connector/Python
Play lesson

Database Systems with SQL - Full Course - Database Systems - Python Database Programming - Connector/Python

5.0 (0)
6 learners

What you'll learn

This course includes

  • 4.5 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Connector/Python is a MySQL component based on the DB-API standard from the Python Software Foundation Setup your Python program for database programming: Install the Connector/Python with command: pip install mysql-connector-python Import mysql.connector Connect with mysql.connector.connect() within a try-except statement Close the connection with connection.close() The MySQLCursor class creates a cursor object that can execute SQL statements and stores the results. connection.cursor() - creates the cursor for the database connection object. The same cursor is used for all your SQL statements for that database connection. cursor.execute()  - executes the SQL statements, passed as a string parameter, after they are compiled cursor.close()  - releases and closes the cursor object. This should be done after any commit or rollback, but before closing the database connection. connection.commit() - saves the results of SQL statements such as INSERT, DELETE, or UPDATE connection.rollback() - discards database changes cursor.rowcount - returns the number of rows in the query result. cursor.column_names - returns a list of column names in a query result. cursor.fetchwarnings() - returns a list of warnings that the query generated. Query parameters are used to substitute Python values into a SQL statement. To pass query parameters in the cursor.execute() method call, the second argument can have %s placeholders passed to it, with a python tuple containing the values in the correct order. The data types of the python values get converted to the correct MySQL data type. It’s better to use placeholders with cursor.execute() rather than to just write the SQL statement with the actual data, because it could prevent a SQL injection attack, where a hacker can try to change the values of the SQL statements. Fetch methods access the query results of SELECT statements when you call the cursor.execute() method. cursor.fetchone() - returns a tuple containing a single result row. It returns None if there are no rows are selected. If there are many rows, cursor.fetchone() can be executed in a loop until None is returned and the loop ends. cursor.fetchall() - returns a list of tuples containing the results of all rows. You can loop through the tuple list to process it. Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

FAQs

Course Hive
Download CourseHive
Keep learning anywhere