sqlite3, MariaDB (mysql), MongoDB (NoSQL) with Python
Integrating and processing data from Data Base (Internal DBMS, Maria DB and MongoDB) with Python
Building Working Environment
MariaDB
MariaDB, a Data Base Management System (DBMS) developed by mysql developers, was installed. Also, mysql connector was installed. The following screenshot highlights the successful installment of MariaDB.
MongoDB
Like other NoSQL databases, MongoDB supports dynamic schema design, allowing the documents in a collection to have different fields and structures. Instead of using tables and rows as in relational databases, MongoDB is built on an architecture of collections and documents. Documents comprises sets of key-value pairs and are the basic unit of data in MongoDB.
The following consoles highlight successful installment of MongoDB.
*Daemon program must first be initiated in order to initiate MongoDB server.
Integrating and Processing sqlite3 database with Python
In order to utilize sqlite3 with Python, the package 'sqlite3' was imported. The version of the sqlite3 is 3.8.11 as shown below.
After importing the package, a new database that can utilized with SQL was created by typing in the followings:
conn = sqlite3.connect('sample_data')
cursor = conn.cursor() #This allows the use of SQL
conn.commit()
Using the SQL syntax, a sqlite3 database was created and new contents were inserted to the database.
After creating the database, the following codes, 'cursor.close()' and 'conn.close()' were inserted to notify all the works are complete.
The following screenshot highlights the newly created database 'sample_data' and the process of integrating the sqlite3 database with python.
In order to read contents from the newly created sqlite3 database, for loop statement can be used. The process of reading contents from the database, 'sample_data', are shown below:
Integrating and Processing MariaDB with Python
Firstly, a new database, 'New_DB', was created in MariaDB using MySQL Client console. Furthermore, a table, 'items', containing records was created in the newly created database, 'New_DB'. Through the use of console, a user, 'DongJun', who will access the database was created. The following screenshots highlight the process.
*Initiating MariaDB and current database in MariaDB
*Creating a new database
*Creating a new table and inserting records in the database
*Creating a user who can access MariaDB
After creating the database in MariaDB, the database can be integrated with Python as shown below.
Through the use of Python, a new record can be added and the existing record can be fixed as well. Also, the records can be deleted and the records can be searched with condition. The followings highlight the aforementioned points.
*Inserting Values
*Fixing Record
*Deleting Record
*Reading Record with condition
Integrating and Processing MongoDB with Python
Firstly, 'DongJunDB' database was created in MongoDB and 'profile' database was created within 'DongJunDB'. Within profile, the following document was inserted: { 'name' : 'Dong Jun', 'age': 22, 'gender': 'male', 'school': 'Hong Kong University of Science and Technology'}
The following highlights this.
After creating a database, MongoDB can be integrated with Python as shown below.
As far as 'profile' database within 'DongJunDB' is concerned, the document content still exists and it can be read through the use of python as shown below.
New documents can be added to the 'profile' database through the use of Python. The following screenshot highlights this.
A document within the database can be removed and the following demonstrates this.
Using MongoDB (NoSQL) syntax, conditional query can be done.
For example, if we want to find documents that contain 'Company': 'Bloomberg' key-value pairs,
'{ <field>: { $eq:<value> } }' syntax can be used.
The following demonstrates the conditional query in NoSQL syntax.
0コメント