import pandas as pd
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=RONSQLEXPRESS;'
'Database=test_database;'
'Trusted_Connection=yes;')
df = pd.read_sql_query('SELECT * FROM products', conn)
print(df)
print(type(df))
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
#create database in python with mysql
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
import sqlite3
try:
sqliteConnection = sqlite3.connect('test.db')
cursor = sqliteConnection.cursor()
print("SQLITE Connection Established!")
cursor.close()
except sqlite3.Error as error:
print("Error while connecting to sqlite", error)
finally:
if (sqliteConnection):
sqliteConnection.close()
print("Connection closed")