MSSQL in python 2.7 - python

Is there a module available for connection of MSSQL and python 2.7?
I downloaded pymssql but it is for python 2.6. Is there any equivalent module for python 2.7?
I am not aware of it if anyone can provide links.
Important note: in the meantime there is a pymssql module available. Don't miss to read the answer at the end of this page: https://stackoverflow.com/a/25749269/362951

You can also use pyodbc to connect to MSSQL from Python.
An example from the documentation:
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
print row.user_id, row.user_name
The SQLAlchemy library (mentioned in another answer), uses pyodbc to connect to MSSQL databases (it tries various libraries, but pyodbc is the preferred one). Example code using sqlalchemy:
from sqlalchemy import create_engine
engine = create_engine("mssql://me:pass#localhost/testdb")
for row in engine.execute("select user_id, user_name from users"):
print row.user_id, row.user_name

If you're coming across this question through a web search, note that pymssql nowadays does support Python 2.7 (and 3.3) or newer. No need to use ODBC.
From the pymssql requirements:
Python 2.x: 2.6 or newer. Python 3.x: 3.3 or newer.
See http://pymssql.org/.

Install pyodbc using pip as follows: pip install pyodbc
import pyodbc
cnxn = pyodbc.connect("DRIVER={SQL Server};SERVER=SOME-PC;DATABASE=my_db")
cursor = cnxn.cursor()
cursor.execute("insert into test_tb values(6, 'name')")
cursor.execute("select id, name from my_tb")
rows = cursor.fetchall()
for row in rows:
print row.id, row.name
For details, see
https://github.com/mkleehammer/pyodbc/wiki

You can try out SQLAlchemy:
The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes (objects) with rows in their corresponding tables.
You can refer following links:
1> http://www.sqlalchemy.org/docs/
2> http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html

Related

SQLite3 syntax error on tested sql script

I'm using python 3.6.4 and sqlite3 2.6.0 to query the nearest consecutive dates in my table in a sqlite 3.27.2 file.
I've tried to get the actual sql string with vscode debugger and test it with DB Browser for SQLite. It works as I expect.
Here's the code:
sql = 'WITH \
dates(cast_date) AS (\
SELECT DISTINCT play_date\
FROM TimeTable\
),\
groups AS (\
SELECT\
date(cast_date, \'-\'||(ROW_NUMBER() OVER (ORDER BY cast_date))||\' days\') AS grp,\
cast_date\
FROM dates\
)\
SELECT\
MIN(cast_date) AS date_start,\
MAX(cast_date) AS date_end\
FROM groups GROUP BY grp ORDER BY 2 DESC LIMIT 1'
cursor = conn.cursor()
result = []
try:
cursor.execute(sql)
result = cursor.fetchone()
except sqlite3.OperationalError:
FileLogger.exception('Exception at '+__file__+' '+__name__)
An exception occurs:
cursor.execute(sql)
sqlite3.OperationalError: near "OVER": syntax error
Window functions support was first added to SQLite with release version 3.25.0 (2018-09-15), according to official documentation.
When using Python, you are using Python SQLite3 client library (which is distributed with Python) instead of your system SQLite3 installation. For Python 2.7, the version is 3.11.0, which is below your required version.
You may try using a newer SQLite3 client library, as suggested by these answers.

unable to use sql built in functions in PYODBC SQL Query from Lotus Note DB

I am new to using 'pyodbc' for querying data from ODBC DB. Specifically a Lotus Notes DB.
This is an example where the query fails using a function in SQL:
import pyodbc
import pandas as pd
cnxn = pyodbc.connect("Driver={Lotus Notes SQL Driver (*.nsf)};SERVER=server;DATABASE=db.nsf;PWD=xxxxx;UID=userid", autocommit=True)
cursor = cnxn.cursor()
sql_addon = """SELECT REPLACE(timestamp_DT,'-','') as timestamp_DT
FROM ViewInNoteDB
"""
df_addon = pd.read_sql(sql_addon, cnxn)
This the error I get:
': ('37000', u"[37000] [Lotus][ODBC Lotus Notes]Name, constant, or expression expected (23008) (SQLExecDirectW); [37000] [Lotus][ODBC Lotus Notes]Incorrect syntax near 'SELECT' (23064)")
I get different errors using GETDATE(), CONVERT function, and many other functions.
It seems that the issue is related to using to SQL*Server syntax which is not supported by the Lotus Notes ODBC driver. CAST and CONVERT are not supported unfortunately.
The only supported column functions: http://www-12.lotus.com/ldd/doc/notessql/2.0.6/notessql.nsf/66208c256b4136a2852563c000646f8c/1f3d9225b5e6a547852567010067254d?OpenDocument

convert parameterized queries from psycopg2 to pyodbc

I use psycopg2 code to open an sql file (postgresql), and I pass the value for LIMIT as a parameter in my code. And it works fine. Here is some of the codes:
.
.
cur.execute(open("sample.sql","r").read(),(lim,))
.
.
my sample.sql creates a table named sample and populates it with some data from another table named test
CREATE TABLE sample(name varchar(500));
INSERT INTO sample(name) SELECT name FROM test LIMIT %s;
%s takes the value lim passed from cur.execute
My question is: how do I translate this into pyodbc code to use it in sql server?
psycopg2 and pyodbc both implement Python's DB API, and that specification defines several parameter styles that can be used. The developers of psycopg2 and pyodbc simply chose different styles:
psycopg2 uses "format" placeholders: ... WHERE city = %s
pyodbc uses "qmark" placeholders: ... WHERE city = ?

Can't use column name list to create SQLite View in Python

I've created a database and am able to create a simple view using the following command:
CREATE VIEW IF NOT EXISTS monthly_terminals (year, month) AS
SELECT reportYear, reportMonth FROM osMonthlyTerminals
This command works just fine if I paste into the SQLite Manager plugin on FireFox:
I then drop this view and try to use the exact same command in the following Python 3 code to create this view...
import sqlite3
db_name = "../data/OsReportMerchants.sqlite"
conn = sqlite3.connect(db_name)
cur = conn.cursor()
cur.execute("CREATE VIEW IF NOT EXISTS monthly_terminals (year, month) AS SELECT reportYear, reportMonth FROM osMonthlyTerminals")
... but I get the following error:
sqlite3.OperationalError: near "(": syntax error
More specifically, it looks like this in ipython:
Yes, I know I should be parameterizing this and I have tried to do so, but keep running into this same snag.
I look at this: https://www.sqlite.org/lang_createview.html and it says that column lists are supported in 3.9 and later, so I updated to version 3.13 of sqlite3 and still ran into this issue.
When I remove the column list and use this:
import sqlite3
db_name = "../data/OsReportMerchants.sqlite"
conn = sqlite3.connect(db_name)
cur = conn.cursor()
cur.execute("CREATE VIEW IF NOT EXISTS monthly_terminals AS SELECT reportYear, reportMonth FROM osMonthlyTerminals")
it works fine. Not sure why it works in one context and not in the other.
Python has its own copy of the SQLite library (see sqlite3.sqlite_version).
If the latest Python version does not have a recent enough version of SQLite, the only way to update it would be to recompile Python, or use some other DB driver like APSW.

Is there any pywin32 odbc connector documentation available?

What is a good pywin32 odbc connector documentation and tutorial on the web?
Alternatives:
mxODBC by egenix.com (if you need ODBC)
pyODBC
sqlalchemy and DB-API 2.0 modules (which isn't ODBC) but it's maybe better alternative
The answer is: 'there isn't one'. However, here is an example that shows how to open a connection and issue a query, and how to get column metadata from the result set. The DB API 2.0 specification can be found in PEP 249.
import dbi, odbc
SQL2005_CS=TEMPLATE="""\
Driver={SQL Native Client};
Server=%(sql_server)s;
Database=%(sql_db)s;
Trusted_Connection=yes;
"""
CONN_PARAMS = {'sql_server': 'foo',
'sql_db': 'bar'}
query = "select foo from bar"
db = odbc.odbc(SQL2005_CS_TEMPLATE % CONN_PARAMS)
c = db.cursor()
c.execute (query)
rs = c.fetchall() # see also fetchone() and fetchmany()
# looping over the results
for r in rs:
print r
#print the name of column 0 of the result set
print c.description[0][0]
#print the type, length, precision etc of column 1.
print c.description[1][1:5]
db.close()
The only 'documentation' that I found was a unit test that was installed with the pywin32 package. It seems to give an overview of the general functionality. I found it here:
python dir\Lib\site-packages\win32\test\test_odbc.py
I should also point out that I believe it is implements the Python Database API Specification v1.0, which is documented here:
http://www.python.org/dev/peps/pep-0248/
Note that there is also V2.0 of this specification (see PEP-2049)
On a side note, I've been trying to use pywin32 odbc, but I've had problems with intermittent crashing with the ODBC driver I'm using. I've recently moved to pyodbc and my issues were resolved.

Categories