BULK INSERT into SQL Server table using pyodbc: cannot find file - python

I know this kind of question has been asked before but still couldn't find the answer I'm looking for. I'm doing bulk insert of the csv file into the SQL Server table but I am getting error shown below:
My Code:
df_output.to_csv('new_file_name.csv', sep=',', encoding='utf-8')
conn = pyodbc.connect(r'DRIVER={SQL Server}; PORT=1433; SERVER=Dev02; DATABASE=db;UID='';PWD='';')
curr = conn.cursor()
print("Inserting!")
curr.execute("""BULK INSERT STG_CONTACTABILITY_SCORE
FROM 'C:\\Users\\kdalal\\callerx_project\\caller_x\\new_file_name.csv'
WITH
(
CODEPAGE = 'ACP',
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);""")
conn.commit()
The Error:
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL
Server Driver][SQL Server]Cannot bulk load because the file
"C:\Users\kdalal\callerx_project\caller_x\new_file_name.csv"
could not be opened. Operating system error code 3(The system cannot
find the path specified.). (4861) (SQLExecDirectW)')
'new_file_name.csv' is in the specified path. I tried changing the path to just 'new_file_name.csv' since it is in the folder from where I am running the script still it throws a
file does not exists
Can you please tell me what am I doing wrong here. Thanks a lot in advance.

The BULK INSERT statement is executed on the SQL Server machine, so the file path must be accessible from that machine. You are getting "The system cannot find the path specified" because the path
C:\\Users\\kdalal\\callerx_project\\caller_x\\new_file_name.csv
is a path on your machine, not the SQL Server machine.
Since you are dumping the contents of a dataframe to the CSV file you could simply use df.to_sql to push the contents directly to the SQL Server without an intermediate CSV file. To improve performance you can tell SQLAlchemy to use pyodbc's fast_executemany option as described in the related question
Speeding up pandas.DataFrame.to_sql with fast_executemany of pyODBC

Related

Python error: unpack requires a buffer of 4 bytes, when using PyMySQL,

I am connecting a Python program in Visual Studio Code to a SQL databse stored in MySQL Workbench 8.0. I am using the PyMySQL connector to do this. However, I am running into an error with code that I have used from another question that I have posted. Here is the link and the code:
How do I connect a Python program in Visual Studio Code to MySQL Workbench 8.0?
pip install PyMySQL
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))
Here is a screenshot with my code and the error that I received.
I tried the code that I recieved from my previous question, but it resulted in an another error. I am pretty sure I have copied the code correctly and the database details. I have researched the error but have been unable to find its relevance to connecting Python programs to MYSQL Workbench 8.0 with PyMySQL.
First of all, obviously you didn't copy the code in the answer correctly.
Your code has the following errors (only from the picture, I don't know what your complete code looks like)
The port number is 3306. NOT 33060. Of course, if you make changes when you install the database, you need to change it to the port number you use.
The fetchall method in data = cur.fetchall does parentheses. It shoud be data = cur.fetchall().
At the moment it seems that the error in the picture is due to the port number.
Modifying to the correct port number will remove this error.

Setting file name as variable with pyodbc, then running program as executable in data storage program instead of through Python

I have created a program that calls a specific data set from Microsoft Access and applies some small changes and then creates a csv file.
However, currently the program needs to have the file name manually inputted into the code. When I try to create a variable and input it when pyodbc asks for the file to be called, the program returns an error.
sql_query = pd.read_sql_query('SELECT * FROM BMDL_SFAM_Final', conn)
df = pd.DataFrame(sql_query)
# Creating the cursor that allows us to select data with pyodbc
cur = conn.cursor()
cur.execute('SELECT * FROM BMDL_SFAM_Final')
My first question is; is there a way to set the file name to a variable at the start of the program, and then just call that variable each time the program asks for the file name? I have tried setting a variable and also tried using .format(), each time getting the same error:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '* BDML_SFAM_Final'. (-3100) (SQLExecDirectW)")
My next step is to create an executable that can be called in the program where I store data. I don't want to have to open my python program to input the file name. Is there a way to have the file name selected when I select the file in the data storage software? The data storage software is called Element.
I found an answer to my own question.
You set your variable to the value you need for your database, query etc.
Then when you call your variable in the connection string or the query selection string you surround the variable with quotations and plus signs:
example: '+database+'
My code looks like this.
database = "C:\Element\Temp\outCCT.mdb"
query = "BMDL_SFAM_Final"
conn_str = (r'Driver={Microsoft Access Driver (*.mdb)};'
r'Server=(local);'
r'DBQ='+database+'; ')
conn = pyodbc.connect(conn_str)
except pyodbc.Error as e:
print("Error in Connection", e)
# Reading the Query and putting it into a panda Database
sql_query = pd.read_sql_query('SELECT * FROM '+query+'', conn)
df = pd.DataFrame(sql_query)
I found this help from the microsoft website:
https://learn.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-ver15#next-steps

Can't connect to SQL Server DB from Pyodbc

I am having a difficult time trying to connect to a SQL Server DB on Linux, using pyodbc. I have a ODCINI file entry created. I started with this:
import pyodbc
conn = pyodbc.connect('DSN=DSN;Database=DB;UID=UID;PWD=PWD')
cursor = conn.cursor()
cursor.execute('SELECT count(*) FROM dbo.tableA')
for row in cursor.fetchall():
print(row)
which throws this error:
RuntimeError: Unable to set SQL_ATTR_CONNECTION_POOLING attribute.
I googled that error and added this line after reading some recommendations:
pyodbc.pooling=False
So script changed to this:
import pyodbc
pyodbc.pooling=False
conn = pyodbc.connect('DSN=DSN;Database=DB;UID=UID;PWD=PWD')
cursor = conn.cursor()
cursor.execute('SELECT count(*) FROM dbo.tableA')
for row in cursor.fetchall():
print(row)
Which resulted in this:
pyodbc.InterfaceError: ('IM003', '[IM003] 䑛瑡䑡物捥嵴佛䉄⁃楬嵢匠数楣楦摥搠楲敶\u2072潣汵\u2064潮⁴敢氠慯敤d\uffff\uffff㢸ꔻ罱\x00\ue5b8鮫罱\x00㳰ꔻ罱\x00\uffff\uffff罱\x00\x00\x00\x00\x00鳭ꕞ罱\x00塰ꕉ罱 (0) (SQLDriverConnect)')
At the suggestion of a coworker I added these 2 lines AFTER the pyodbc.connect line:
conn.setdecoding(pyodbc.SQL_CHAR, encoding='latin1', to=str)
conn.setencoding(str, encoding='latin1')
I tried that with both latin1 and utf-8. Neither work, still throws the same interface error with Chinese characters.
Any ideas?
I had the similar issue with same description RuntimeError: Unable to set SQL_ATTR_CONNECTION_POOLING attribute. I had no clue what is happening and why is happening. After lot of debugging i was able to figure it out why.
Simple answer is :
Reinstall the unixODBC drivers or/and SQL drivers.
Reason why :
When install the ODBC Drivers first and then SQL related drivers, sometimes it override the symlinks in Unix system. You can find out more info on this from pyodbc official GitHub issue#847 .
you can simply uninstall and then do:
conda install unixodbc

sqlite file is shown empty in python and r

I am trying to open a .sqlite3 file in python but I see no information is returned. So I tried r and still get empty for tables. I would like to know what tables are in this file.
I used the following code for python:
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
database = "D:\\...\assignee.sqlite3"
conn = create_connection(database)
cur = conn.cursor()
rows = cur.fetchall()
but rows are empty!
This is where I got the assignee.sqlite3 from:
https://github.com/funginstitute/downloads
I also tried RStudio, below is the code and results:
> con <- dbConnect(drv=RSQLite::SQLite(), dbname="D:/.../assignee")
> tables <- dbListTables(con)
But this is what I get
first make sure you provided correct path on your connection string to the sql
light db ,
use this conn = sqlite3.connect("C:\users\guest\desktop\example.db")
also make sure you are using the SQLite library in the unit tests and the production code
check the types of sqllite connection strings and determain which one your db belongs to :
Basic
Data Source=c:\mydb.db;Version=3;
Version 2 is not supported by this class library.
SQLite
In-Memory Database
An SQLite database is normally stored on disk but the database can also be
stored in memory. Read more about SQLite in-memory databases.
Data Source=:memory:;Version=3;New=True;
SQLite
Using UTF16
Data Source=c:\mydb.db;Version=3;UseUTF16Encoding=True;
SQLite
With password
Data Source=c:\mydb.db;Version=3;Password=myPassword;
so make sure you wrote the proper connection string for your sql lite db
if you still cannot see it, check if the disk containing /tmp full otherwise , it might be encrypted database, or locked and used by some other application maybe , you may confirm that by using one of the many tools for sql light database ,
you may downliad this tool , try to navigate directly to where your db exist and it will give you indication of the problem .
download windows version
Download Mac Version
Download linux version
good luck

SQLite Database and Python

I have been given an SQLite file to exam using python. I have imported the SQLite module and attempted to connect to the database but I'm not having any luck. I am wondering if I have to actually open the file up as "r" as well as connecting to it? please see below; ie f = open("History.sqlite","r+")
import sqlite3
conn = sqlite3.connect("history.sqlite")
curs = conn.cursor()
results = curs.execute ("Select * From History.sqlite;")
I keep getting this message when I go to run results:
Operational Error: no such table: History.sqlite
An SQLite file is a single data file that can contain one or more tables of data. You appear to be trying to SELECT from the filename instead of the name of one of the tables inside the file.
To learn what tables are in your database you can use any of these techniques:
Download and use the command line tool sqlite3.
Download any one of a number of GUI tools for looking at SQLite files.
Write a SELECT statement against the special table sqlite_master to list the tables.

Categories

Resources