django-pyodbc and pervasive database - django-models

Hi There is a way to use django-pyodbc with pervasive database ? I try different settings but when I try to inspectdb to create a model from the database it shows me this error but I can create querys in pervasive with python pyodbc.
`('42000', '[42000] [Pervasive][ODBC Client Interface][LNA][Pervasive][ODBC
Engine Interface]Invalid SET statement. (0) (SQLExecDirectW)')
Request Method: GET
Request URL:
Django Version: 1.6.6
Exception Type: ProgrammingError
Exception Value:
('42000', '[42000] [Pervasive][ODBC Client Interface][LNA][Pervasive][ODBC Engine Interface]Invalid SET statement.
(0) (SQLExecDirectW)')
Exception Location: C:\Python27\lib\site-packages\django_pyodbc\base.py in _cursor, line 296
Python Executable: C:\Python27\python.exe
Python Version: 2.7.8
cursor.execute("SET DATEFORMAT Ymd; SET DATEFIRST %s" % self.datefirst)
pyodbc.ProgrammingError: ('42000', '[42000] [Pervasive][ODBC Client Interface][LNA][Pervasive] [ODBC Engine Interface]Invalid SET statement. (0) (SQLExecDirectW)')`
and this is the piece of the error when I try to run inspectdb on the database.
According to the main problem is in this file base.py line 296 is some problem with a set statement on the odbc driver this is piece of the code on base.py I try to comment but after another thing shows up.
`cursor.execute("SET DATEFORMAT Ymd; SET DATEFIRST %s" % self.datefirst)
if self.ops.sql_server_ver < 2005:
self.creation.data_types['TextField'] = 'ntext'
self.features.can_return_id_from_insert = False
ms_sqlncli = re.compile('^((LIB)?SQLN?CLI|LIBMSODBCSQL)')
self.drv_name = self.connection.getinfo(Database.SQL_DRIVER_NAME).upper()
# http://msdn.microsoft.com/en-us/library/ms131686.aspx
if self.ops.sql_server_ver >= 2005 and ms_sqlncli.match(self.drv_name) and self.MARS_Connection:
# How to to activate it: Add 'MARS_Connection': True
# to the DATABASE_OPTIONS dictionary setting
self.features.can_use_chunked_reads = True`

Try put single quote around %s:
cursor.execute("SET DATEFORMAT Ymd; SET DATEFIRST '%s'" % self.datefirst)

Check your access rights. The error message indicates a syntax error but according to mssql support site, error code 42000 is either "Syntax error or access violation".

Related

Could not change transaction status (0) (SQLSetConnectAttr)

We have a Django app connected to a remote SQL-server database.
I get an error when i try to do an update request on a database table :
[HY000] [FreeTDS][SQL Server]Could not change transaction status (0) (SQLSetConnectAttr).
I conclude that a similar request is still in progress and it blocks all my requests.
1/ The "autocommit" parameter is True.
2/ I tested :
connection.rollback()
This did not resolve the problem.
3/ I tried to do a live SQL query on the database.
My query returns no error.
It's a problem with Django or the libraries used.
Traceback :
/venv/src/django-pyodbc-azure/sql_server/pyodbc/base.pyc in
_set_autocommit(self, autocommit)
361 else:
362 self.connection.rollback()
--> 363 self.connection.autocommit = autocommit
364
365 def check_constraints(self, table_names=None):
Error: ('HY000', '[HY000] [FreeTDS][SQL Server]Could not change transaction status (0) (SQLSetConnectAttr)')
Does someone have an idea ?
The problem is fixed.
I remove the following line:
if not ModelName.objects.filter(pk=instance.pk)
My code :
#receiver(pre_save, sender=ModelName)
def model_name_pre_save(sender, instance, **kwargs):
if not ModelName.objects.filter(pk=instance.pk):
instance.field_name = value

pyodbc+mssql connection possible but query fails

The following code is running into a Docker container. I have a connection specified as follow which is working as I could make a simple query based on it.
from sqlalchemy import create_engine
engine = create_engine("mssql+pyodbc://username:pw#hostname?driver_name")
con_xpf = engine.connect()
con_xpf.execute("use db_name;")
After that I create a sqlite3 DB and connect to it:
DBNAME = "data/NEWDB.db"
con = sqlite3.connect(DBNAME)
tbllist = ["AAAAA","BBBBBBBB","CCCCCCCCC","DDDDDDDDD","EEEEEEEEE"
"FFFFFFFFF","GGGGGGGGG","HHHHHH","IIIIIIIII"]
chunksizes = [500000,800000,500000,1000000,500000,500000,500000,900000,500000]
The problem start from here when I try to run the following code to query from the first DB to write into the sqlite DB:
def loads_data_from_xpf(tbllist,chunksizes) :
for tbl,chunksize in zip(tbllist,chunksizes) :
cnt = 0
maxcnt = math.ceil(pd.read_sql("SELECT COUNT(*) CNT FROM x.{}".format(tbl),con_xpf).CNT[0]/chunksize)
with tqdm(range(maxcnt)) as pbar:
for chunk in pd.read_sql("SELECT * FROM x.{} A".format(tbl),con_xpf,chunksize=chunksize) :
chunk.to_sql(tbl,con,if_exists="append")
pbar.update()
loads_data_from_xpf(tbllist,chunksizes)
I got the following error:
OperationalError: (pyodbc.OperationalError) ('08S01', '[08S01] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x71 (113) (SQLGetData)')
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Could the problem be because of the size of the query? Do you see any error somewhere? I am stuck for a while. Any help would be appreciated.
EDIT
I am now able to see the sqlite DB file updating meaning data are pushed into it. But I have the following error which is because of the Jupyter Kernel. Help please
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.6/site-packages/nbclient/client.py", line 841, in async_execute_cell
exec_reply = await self.task_poll_for_reply
concurrent.futures._base.CancelledError
During handling of the above exception, another exception occurred:
nbclient.exceptions.DeadKernelError: Kernel died

Perl DBI / MS ODBC Driver (LinuxL:RHEL) / SQL-Server: How to insert/update BLOB varbinary(max) data?

New to SQL-Server. I'm attempting to load a pdf to a SQL-Server table (data type varbinary(max)) via PERL/MS ODBC driver/DBD::ODBC using the following (simplified) code:
use DBI qw(:sql_types);
open my $pdfFH, "test.pdf";
my #pdf = <$pdfFH>; close $pdfFH;
my $pdfStr = join('', #pdf);
my $dbh = <...valid db-handle ...>;
my $sth = $dbh->prepare(qq(
insert into
TestTable(Report)
values
(?)));
$sth->bind_param(1,$pdfStr,DBI::SQL_VARBINARY);
$sth->execute;
Error:
DBD::ODBC::st bind_param failed: [Microsoft][ODBC Driver 17 for SQL Server]Invalid precision value (SQL-HY104) at ./t_sqlserver.pl line 37.
DBD::ODBC::st execute failed: [Microsoft][ODBC Driver 17 for SQL Server]COUNT field incorrect or syntax error (SQL-07002) at ./t_sqlserver.pl line 38.
I am able to successfully load other data types. An alternative is to load the pdf locally from the file system using OPENROWSET(BULK...) but I would prefer to load directly to avoid moving the file from Linux to Windows.
The driver should be clever enough to guess the correct type most of the times. Try binding the parameter without specifying the type at all.

show printed messages inside stored procedure

is it possible to see stdout (or log) T-SQL PRINT messages embedded in stored procedures when executed using sqlalchemy?
--edit--
It looks like pyodbc added a way to retrieve messages from a cursor for this very reason and it has been merged to master. I still cant figure out how to get this to bubble up to sqlalchemy
https://github.com/mkleehammer/pyodbc/pull/765#
--endedit--
I tried this adding echo=True to engine and setting a log level to debug but I can not see anything
this is how I am executing the usp
import sqlalchemy as sa
import logging
logging.basicConfig()
logger = logging.getLogger("sqlalchemy.engine")
logger.setLevel(logging.DEBUG)
engine = sa.create_engine(target_uri, echo=True)
statement = sa.sql.text(f"EXEC {usp_schema}.{usp_name}")
with engine.begin() as cnxn:
cnxn.execute(statement)
CREATE PROCEDURE dbo.usp_do_stuff
BEGIN
BEGIN TRAN
PRINT 'DELETE STAGE'
PRINT 'LOAD STAGE'
PRINT 'CLEAN STAGE'
PRINT 'TRANSPORT STAGE'
END TRAN
END;
https://github.com/mkleehammer/pyodbc/pull/765
has only recently been merged (2021-01-21) and SQLAlchemy's mssql+pyodbc:// dialect does not know anything about it yet. If you would like to see support for that feature added to SQLAlchemy then please open a feature request here.
In the meantime, the workaround is to install pyodbc from the current master branch …
pip install git+https://github.com/mkleehammer/pyodbc.git
… and then use a .raw_connection() to execute the SP and retrieve the messages:
"""existing stored procedure:
CREATE PROCEDURE [dbo].[usp_do_stuff]
AS
BEGIN
BEGIN TRANSACTION
PRINT 'DELETE STAGE'
PRINT 'LOAD STAGE'
PRINT 'CLEAN STAGE'
PRINT 'TRANSPORT STAGE'
COMMIT TRANSACTION
END
"""
cnxn = engine.raw_connection()
crsr = cnxn.cursor()
crsr.execute("EXEC usp_do_stuff")
print(crsr.messages)
while crsr.nextset():
print(crsr.messages)
"""
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]DELETE STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]LOAD STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]CLEAN STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]TRANSPORT STAGE')]
"""

codeigniter mysql session cookie issue

I am upgrading ci 2 to ci 3.11
I have done all the changes in upgrading,
Still i get this error
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id = 'gr6rnf66t7d20oqlkocr5ate3nlg3equ'' at line 2
SELECT 1 WHERE id = 'gr6rnf66t7d20oqlkocr5ate3nlg3equ'
Filename: D:/xampp/htdocs/XXXX/system/database/DB_driver.php
Line Number: 691
I want to know from while file this database querying.
Just now i found that if i change
$config['sess_cookie_name'] = 'ci_session';
this value id in the query is changing
For now i changed to sess_driver to files in config to work
I didn't give $config['sess_save_path'] = 'ci_sessions'; (Its the table name of session)

Resources