Answers for "sql query in python"

2

python query mssql

import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'   
driver= '{ODBC Driver 17 for SQL Server}'

with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
    with conn.cursor() as cursor:
        cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
        row = cursor.fetchone()
        while row:
            print (str(row[0]) + " " + str(row[1]))
            row = cursor.fetchone()
Posted by: Guest on December-03-2020
0

execut sql python

# connect database and create cursor here
import sqlite3 
conn = sqlite3.connect('File.Name')
cur = conn.cursor()
# statement to be executed
cur.execute("""SELECT COL_NAME FROM TABLE_NAME LIMIT 5;""").fetchall()
Posted by: Guest on June-25-2020
1

python sql

C:UsersYour NameAppDataLocalProgramsPythonPython36-32Scripts>python -m pip install 
  mysql-connector-python
Posted by: Guest on March-17-2021

Python Answers by Framework

Browse Popular Code Answers by Language