Category: Python

Python and MongoDB Operations. Code Example

#!/usr/bin/env python# coding: utf-8# In[63]: import pymongo; # In[64]: import pymongomyclient = pymongo.MongoClient(“mongodb://localhost:27017/”)mydb = myclient[“mydatabase”] # In[65]: # Check if database exist # In[66]: print(myclient.list_database_names()) # In[67]: dblist = myclient.list_database_names()if “mydatabase” in dblist:  print(“The database exists.”) # In[68]: # Create a collection # In[69]: import pymongomyclient = pymongo.MongoClient(“mongodb://localhost:27017/”)mydb = myclient[“mydatabase”]mycol = mydb[“customers”] # In[70]: #collection …

Continue reading

Python and MySQL Operations. Code Examples

#!/usr/bin/env python# coding: utf-8# In[1]: import mysql.connector # In[2]: # create a database connection # In[3]: import mysql.connectormydb = mysql.connector.connect(  host=”localhost”,  user=”root”,  password=””)print(mydb) # In[4]: import mysql.connectormydb = mysql.connector.connect(  host=”localhost”,  user=”root”,  password=””)mycursor = mydb.cursor()mycursor.execute(“drop DATABASE mydatabase”);mycursor.execute(“CREATE DATABASE mydatabase”); # In[5]: # check if database exists # In[6]: import mysql.connectormydb = mysql.connector.connect(  host=”localhost”,  user=”root”,  password=””)mycursor = …

Continue reading