import MySQLdb # Open database connection db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="testdb") # prepare a cursor object using cursor() method cursor = db.cursor() # execute SQL query using execute() method. cursor.execute("SELECT VERSION()") # Fetch a single row using fetchone() method. data = cursor.fetchone() print "Database version : %s " % data # disconnect from server db.close()
import psycopg2 # Open database connection db = psycopg2.connect(database="testdb", user="postgres", password="password", host="localhost", port="5432") # Create a cursor connection cursor = db.cursor() # Execute SQL query using execute() method. cursor.execute("SELECT VERSION()") # Fetch a single row using fetchone() method. data = cursor.fetchone() print "Database version : %s " % data # close the cursor cursor.close() # close the database connection db.close()
import sqlite3 # Open database connection db = sqlite3.connect('test.db') # Create a cursor connection cursor = db.cursor() # execute SQL query using execute() method. cursor.execute('''SELECT SQLITE_VERSION()''') # Fetch a single row using fetchone() method. data = cursor.fetchone() print "SQLite version: %s" % data # close the cursor cursor.close() # close the database connection db.close()
import pymongo # Open database connection client = pymongo.MongoClient("mongodb://localhost:27017/") # Get the database db = client["testdb"] # Get the collection col = db["testcol"] # Find one document doc = col.find_one() print(doc) # close the database connection client.close()