Search
 
SCRIPT & CODE EXAMPLE
 

SQL

python how to connect to sql server

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))
Comment

connecting to mysql database using python

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")
Comment

how to connect sql database in python

#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")
Comment

connect to sql database python

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")
Comment

PREVIOUS NEXT
Code Example
Sql :: SQL Multiple Cases 
Sql :: mysql delete duplicates 
Sql :: python sqlite3 update 
Sql :: json extract 
Sql :: 3rd highest value in sql 
Sql :: oracle drop sequence 
Sql :: update with inner join sql server 
Sql :: sql row number in result set 
Sql :: sqlite3 import csv 
Sql :: launch sql script from docker in mysql 
Sql :: sql server add time to date 
Sql :: delete a temporary table mysql 
Sql :: how to check table name in current database sql 
Sql :: psql execute sql file 
Sql :: mysql remove auto increment 
Sql :: sql character index 
Sql :: oracle index size 
Sql :: mysql identified by syntax error 
Sql :: create temp table in sql 
Sql :: set all auto_increment values in sql 
Sql :: login phpmyadmin without password 
Sql :: unique key in ms sql server 
Sql :: services.AddDbContext DataSource Sqlite 
Sql :: SQL CASE With ELSE in SQL 
Sql :: PL SQL VARRAY of records 
Sql :: mysql random 
Sql :: sql column to row 
Sql :: declare value in sql 
Sql :: sqlite3 pragma foreign keys 
Sql :: sql online 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =