Python script hangs when I run it. Not sure what is getting it stuck. Just had a battle with having to install new ODBC Drivers for postgres, could that be the reason?
import pyodbc
server = 'localhost'
database = 'screens_database'
username = 'postgres'
password = ''
driver='{ODBC Driver 17 for SQL Server}'
cxcn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=5000;DATABASE='+database+';UID='+username+';PWD='+password)
#cursor
cur = con.cursor()
cur.execute("select * from screens_database")
con.close()
Related
I am creating a program that uses VS Code and MySQL Workbench 8.0 together. I am stuck and do not know how to connect the two software together
I also have to be able to upload records into a table that is stored in MySQL Workbench from the Python program that uses variables.
Please tell me if their are any details missing.
Thank you.
For connection:
I have researched on Google and have been unable to find an answer. I have found that I have to install certain packages and use the connect method. However, I do not know the parameters of the connect function.
For uploading data into table:
I have found that I have to create a cursor to somehow upload the data to the table, but am unsusre of the full details.
There are many packages in python that can connect to the mysql database, here we take pymysql as an example.
Install pymysql
pip install PyMySQL
I have already installed, so the prompt package already exists.
Sample code, query and insert data
import pymysql
con = pymysql.Connect(
host='localhost',
port=3306,
user='root',
password='123456',
db='test',
charset='utf8'
)
cur = con.cursor()
sql1 = 'select * from student'
cur.execute(sql1)
data = cur.fetchall()
cur.close()
con.close()
for i in data:
print(str(i))
Add an insert data statement, and re-query after inserting data.
import pymysql
con = pymysql.Connect(
host='localhost',
port=3306,
user='root',
password='123456',
db='test',
charset='utf8'
)
cur = con.cursor()
sql2 = 'insert into student values("002","jerry","W");'
cur.execute(sql2)
sql1 = 'select * from student'
cur.execute(sql1)
data = cur.fetchall()
con.commit()
cur.close()
con.close()
for i in data:
print(str(i))
I am trying, for the first time ever, to send data from a data frame in Spyder to Azure SQL Server...I think it's called Synapse. I created a small table in the database and when I run the code below, I see the results I expect to see.
import pyodbc
server = 'ryan-server.database.windows.net'
database = 'ryan_sql_db'
username = 'UN'
password = 'PW'
driver= '{ODBC Driver 17 for SQL Server}'
with pyodbc.connect('DRIVER='+driver+';SERVER=tcp:'+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT * From Order_Table")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
So, the connection is fine. I guess I am just stuck on the syntax to push a data frame to SQL Server in Azure. I tested the code below.
import pyodbc
server = 'ryan-server.database.windows.net'
database = 'ryan_sql_db'
username = 'UN'
password = 'PW'
driver= '{ODBC Driver 17 for SQL Server}'
conn = pyodbc.connect('DRIVER='+driver+';SERVER=tcp:'+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
all_data.to_sql('health', conn, if_exists='replace', index=True)
When I run that code, I get this error.
DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")
Try to do it by importing Pandas and pyodbc.
Below are few basic steps which we follow usually:
Connect to SQL Server.
Install all your python packages in your local.
Load the data into CSV.
Later you can use below Python script to load it from dataframe.
import pyodbc
import pandas as pd
df = pd.read_csv("c:\\user\\username\department.csv")
server = 'yourservername'
database = 'AdventureWorks'
username = 'username'
password = 'yourpassword'
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
#Insert Dataframe into SQL Server:
for index, row in df.iterrows():
cursor.execute("INSERT INTO HumanResources.DepartmentTest (DepartmentID,Name,GroupName) values(?,?,?)", row.DepartmentID, row.Name, row.GroupName)
cnxn.commit()
cursor.close()
After completing the configs you can run below command to get the data from SQL:
SELECT count(*) from HumanResources.DepartmentTest;
Refer to this official doc for detailed explanation.
I am getting below error
query = command % processed_params TypeError: not all arguments
converted during string formatting
I am trying to pull data from SQL server and then inserting it into Snowflake
my below code
import pyodbc
import sqlalchemy
import snowflake.connector
driver = 'SQL Server'
server = 'tanmay'
db1 = 'testing'
tcon = 'no'
uname = 'sa'
pword = '123'
cnxn = pyodbc.connect(driver='{SQL Server}',
host=server, database=db1, trusted_connection=tcon,
user=uname, password=pword)
cursor = cnxn.cursor()
cursor.execute("select * from Admin_tbldbbackupdetails")
rows = cursor.fetchall()
#for row in rows:
# #data = [(row[0], row[1],row[2], row[3],row[4], row[5],row[6], row[7])]
print (rows[0])
cnxn.commit()
cnxn.close()
connection = snowflake.connector.connect(user='****',password='****',account='*****')
cursor2 = connection.cursor()
cursor2.execute("USE WAREHOUSE FOOD_WH")
cursor2.execute("USE DATABASE Test")
sql1="INSERT INTO CN_RND.Admin_tbldbbackupdetails_ip"
"(id,dbname, dbpath, backupdate, backuptime, backupStatus, FaildMsg, Backupsource)"
"values (?,?,?,?,?,?,?,?)"
cursor2.execute(sql1,*rows[0])
It's obviously string parsing error.
You missed to provide parameter to %s printout.
If you cannot fix it step back and try another approach.
Use another script to achieve the same and get back to you bug tomorrow :-)
My script is doing pretty much the same:
1. Connect to SQL Server
-> fetchmany
-> multipart upload to s3
-> COPY INTO Snowflake table
Details are here: Snowpipe-for-SQLServer
I am having a weird issue with my python script. My script has to connect to MySQL DB. This is the code:
try:
conn = MySQLdb.connect( user='root', host = 'localhost')
cursor = conn.cursor()
databases = cursor.fetchall()
cursor.close()
except Exception as e:
print e
when I run this script I have and error like:
(1045, "Access denied for user 'root'#'localhost' (using password: NO")
in the other hand, I can connect to MySQL just by entering MySQL (without password).
Why am I having this error with my python script when there is no password to root user?
Provide empty password
try this
conn = MySQLdb.connect( user='root', host = 'localhost', passwd='')
This should be the syntax. You should have the MySql connector for Python
import mysql.connector
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='database_name')
cnx.close()
try this (inside the bloc try except)
import mysql.connector
conn = mysql.connector.connect(user='root', password='',host='localhost',
database='your_database_name')
conn.close()
This python script connects to MySQL database and MssQL 2008 R2 database. MySQL database runs on Linux Ubuntu 11.04. MssQL 2008 runs on Windows. The script runs from Linux (Ubuntu 11.04).
#!/usr/bin/python
import pymssql as ms
import MySQLdb as mdb
import sys
//Connection to MSSQL
#Connection to MSSQL
connMSSQL=ms.connect(host='192.168.8.52', user='sa', password='hostailpw321', database='hostail', as_dict=True)
//Connection to MySQL
#Connection to MySQL
connMySQL=mdb.connect('localhost', 'root', 'trail123', 'trail');
//Cursor to MSSQL
#Cursor to MSSQL
curMSSQL=connMSSQL.cursor()
//Cursor to MySQL
#Cursor to MySQL
curMySQL=connMySQL.cursor(mdb.cursors.DictCursor)
curMySQL.execute('SELECT * FROM clinics_mapping')
//Get Data from MySQL
#Get Data from MySQL server
for rowMySQL in curMySQL:
#print (rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])
#curMSSQL.executemany("INSERT INTO clinics values(%s,%s)", [(rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])])
names = rowMySQL['Clinic_name']
codes = rowMySQL['Clinic_code']
qryINS="INSERT INTO clinics(name,code)values('%s','%s')" %(str(names),str(codes))
curMSSQL.execute(qryINS)
#print (rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])
#When I print qryINS I get a query that executes perfect in MSSQL 2008 R2 query editor
print qryINS
//Close MSSQL connection
#Close MSSQL connection
connMSSQL.close()
//Close MySQL connection
#Close MySQL connection
connMySQL.close()
The problem got resolved once I called commit() as follows connMSSQL.commit() immediately after curMSSQL.execute(qryINS) line
#!/usr/bin/python
import pymssql as ms
import MySQLdb as mdb
import sys
#Connection to MSSQL
connMSSQL=ms.connect(host='192.168.8.52', user='sa', password='hostailpw321', database='hostail', as_dict=True)
#Connection to MySQL
connMySQL=mdb.connect('localhost', 'root', 'trail123', 'trail');
#Cursor to MSSQL
curMSSQL=connMSSQL.cursor()
#Cursor to MySQL
curMySQL=connMySQL.cursor(mdb.cursors.DictCursor)
curMySQL.execute('SELECT * FROM clinics_mapping')
#Get Data from MySQL server
for rowMySQL in curMySQL:
#print (rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])
#curMSSQL.executemany("INSERT INTO clinics values(%s,%s)", [(rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])])
names = rowMySQL['Clinic_name']
codes = rowMySQL['Clinic_code']
qryINS="INSERT INTO clinics(name,code)values('%s','%s')" %(str(names),str(codes))
curMSSQL.execute(qryINS)
connMSSQL.commit()
"""
I had not called commit() which persists your data if you had not put autocommit to True.I have called it as connMSSQL.commit() in my script.
"""
#Close MSSQL connection
connMSSQL.close()
#Close MySQL connection
connMySQL.close()
pyodbc works for me. http://sourceforge.net/projects/pyodb/