Path: blob/master/Python core/Connecting Databases using Python.ipynb
3074 views
Connecting to Database
The Python standard for database interfaces is the Python DB-API.In most of the cases one have download a separate DB API module for each database access.
The DB API provides a minimal standard for working with databases using Python structures and syntax wherever possible. This API includes the following −
Importing the API module.
Acquiring a connection with the database.
Issuing SQL statements and stored procedures.
Closing the connection
1. Database Connection: SQlite3
What is SQLite?
SQLite is a relational database management system (In C) ,that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine.
SQLite3 can be integrated with Python using sqlite3 module
No need to install as it shipped by default along with Python version 2.5.x onwards.
To use sqlite3 module, you must first create a connection object that represents the database and then optionally you can create a cursor object, which will help you in executing all the SQL statements.
conda install -c blaze sqlite3
Important APIs
sqlite3.connect(database [,timeout ,other optional arguments])
This API opens a connection to the SQLite database file. You can use ":memory:" to open a database connection to a database that resides in RAM instead of on disk. If database is opened successfully, it returns a connection object.
connection.cursor([cursorClass])
This routine creates a cursor which will be used throughout of your database programming with Python. This method accepts a single optional parameter cursorClass.
cursor.execute(sql [, optional parameters])
This routine executes an SQL statement. The SQL statement may be parameterized (i. e. placeholders instead of SQL literals). The sqlite3 module supports two kinds of placeholders: question marks and named placeholders (named style).
For example − cursor.execute("insert into name values (?, ?)", (who, income))
connection.commit()
This method commits the current transaction. If you don't call this method, anything you did since the last call to commit() is not visible from other database connections.
connection.rollback()
This method rolls back any changes to the database since the last call to commit().
First, we connected to the database by create a new MySQLConnection object
Second, from the MySQLConnection object, we instantiated a new MySQLCursor object
Third, we executed a query that selects all rows from the books table.
Fourth, we called fetchone() method to fetch the next row in the result set. In the while loop block, we printed out the content of the row and move to the next row until all rows are fetched.
Fifth, we closed both cursor and connection objects by invoking the close() method of the corresponding object.
##DELIMITER $$
##USE python_mysql$$
CREATE PROCEDURE find_all() BEGIN SELECT title, isbn, CONCAT(first_name,' ',last_name) AS author FROM books INNER JOIN book_author ON book_author.book_id = books.id INNER JOIN AUTHORS ON book_author.author_id = authors.id; END$$
DELIMITER ;
2. Database Connection : MongoDB
MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemata. (Key : Value)
Why MongoDB?
The ever-increasing need for the ability to store complex data structures led to the birth of NoSQL databases, which allow a developer to store heterogeneous and structure-less data.
MongoDB also provides load balancing support by automatically moving data across the various shards
It has automatic failover support - in case your primary server goes down, a new primary will be up and running automatically
The MongoDB Management Service or MMS is a very nice web tool that provides you with the ability to track your machines
Deployment of MongoDB is very easy
To install this package with conda run
conda install -c anaconda pymongo
link to install MongoDB in windows
https://medium.com/@LondonAppBrewery/how-to-download-install-mongodb-on-windows-4ee4b3493514
Note : MongoDB store data in following form
record = {
title: 'MongoDB and Python',
description: 'MongoDB is no SQL database',
tags: ['mongodb', 'database', 'NoSQL'],
viewers: 104 }