django-pyodbc DatabaseError message is wired - python

I use django-pyodbc on Django 1.6.1
when I run manage.py syncdb, everything is fine.
I use 2 database setttings in Django to read some data from my legacy database, when I what to read the data from a model name T_AllStation:
all_t_station = T_AllStation.objects.using('SQL_Server').all()
An error is raised:
DatabaseError at /company/get_station_info
('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]\xc1\xd0\xc3\xfb 'id' \xce\xde\xd0\xa7\xa1\xa3 (207) (SQLExecDirectW)")
Request Method: GET
Request URL: http://127.0.0.1:8000/company/get_station_info
Django Version: 1.6.1
Exception Type: DatabaseError
Exception Value:
('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]\xc1\xd0\xc3\xfb 'id' \xce\xde\xd0\xa7\xa1\xa3 (207) (SQLExecDirectW)")
Exception Location: E:\VirtualEnvs\EnvMonitor\lib\site-packages\django_pyodbc\base.py in execute, line 416
Python Executable: E:\VirtualEnvs\EnvMonitor\Scripts\python.exe
Python Version: 2.7.2
What does the string \xc1\xd0\xc3\xfb 'id' \xce\xde\xd0\xa7\xa1\xa3 (207) mean ?

Try making your configuration file use options like this, to account for Unicode:
DATABASES = {
'default': {
'ENGINE': 'django_pyodbc',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'your_password',
'HOST': 'database.domain.com,1433',
'PORT': '1433',
'OPTIONS': {
'host_is_server': True,
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0'
},
}
}
(You're on Windows, so you don't need the tds_version, but it can't hurt so your config is portable to Linux.)

Related

How to connect Vertica schema with vertica_python?

I try to connect to vertica DB with 2 schema: 'public' and 'my_schema'
I can only connect to 'public' schema and not to 'my_schema'
import vertica_python
conn_info = {'host': '****',
'port': 5433,
'user': '****',
'password': '****',
'database': 'my_schema',
# 10 minutes timeout on queries
'read_timeout': 600,
# default throw error on invalid UTF-8 results
'unicode_error': 'strict',
# SSL is disabled by default
'ssl': False}
conn = vertica_python.connect(**conn_info)
cur = conn.cursor()
and this is what I get:
vertica_python.errors.ConnectionError: Severity: FATAL, Message: Database "my_schema" does not exist
how can I connect the 'my_schema' with python?
I found the solution,
database should be "public" and the query should be with the schema name:
select * from my_schema.{table name}

How to remove backslash in Django DB settings

I have been trying to link my mssql database to Django. When I run the Django server I get the error below. For normal query out of Django, it works fine, however, the Django doesnt even connect. My normal username when login into windows is kmoh from EMEA domain, when I normaly login into my windows account it looks like this: EMEA\kmoh but as you can see in the error it looks like this EMEA\\kmoh. So How can I remove this another backslash?
File "C:\ProgramData\Anaconda3\lib\site-packages\sql_server\pyodbc\base.py", line 307, in get_new_connection
timeout=timeout)
django.db.utils.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [28000]
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)")
I am using these:
Windows 10
django-pyodbc-azure: version 2.1
Django 2.1
Pyodbc: 4.0.25
my Django settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'xxx', #has been hide for this post
'PORT': '1433',
'NAME': 'BBL_Result',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
I would expect to see the user and password set explicitly in the settings:
DATABASES = {
'default': {
...
'USER': 'db_username',
'PASSWORD': 'db_password',

Using Sql Server with Django 2.0

I would like to use Django 2.0 with legacy MS SQL Server database.
Latest information regarding usage Django with MS SQL Server i could find is Using Sql Server with Django in production/These days and it is about Django 1.11
Most supported seems django-pyodbc-azure but it's not supporting Django 2.0 yet:
django-pyodbc-azure issue #124
Is there any alternative?
Found the solution and post it if anyone facing same problem.
I used django-pyodbc-azure 2.0.4.1 in my Django 2.0.4
The settings that worked for me:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'dbName',
'USER': 'yourUserName',
'PASSWORD': 'yourPassword',
'HOST': '(local)',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 11 for SQL Server',
},
}
}
I was facing same error of ODBC Driver Manager
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manag
er] Data source name not found and no default driver specified (0) (SQLDriverCon
nect)')
Solution: When i added below code in database connection, then my error fixed.
'OPTIONS': {
'host_is_server': True,
'driver': 'ODBC Driver 11 for SQL Server',
}

Error while using MySql with Python 3.4.3 and Django 1.9.6

I have tried using
pymysql
mysql-client
mysql-connector-pyhton
and various other ways stated in this question but still I am not able to use MySql as my database.
Whenever I try to do run it shows below error message.
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
I have tried these two Database configuration in my setting.py file
Setting 1: This works with python 2.7
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'user_db',
'USER': 'ecom',
'PASSWORD': 'ecom#123',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '',
}
}
error message :
`raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'`
Setting 2: as suggested in this question's answer
DATABASES = {
'default': {
'NAME': 'mydatabase',
'ENGINE': 'mysql.connector.django',
'USER': 'myuser',
'PASSWORD': 'secretpassword',
'OPTIONS': {
'autocommit': True,
},
}
}
error message :
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: No module named 'mysql'
Still I am not able to use mysql as backend can anyone please let me know what I am doing wrong or is it even possible with python 3.4.3?
I would be really thankful for your help and guidance.

Trouble using django-pyodbc with python manage.py dbshell / loaddata

I'm having trouble connecting to a SQL Server database through python manage.py dbshell / loaddata.
I'm set up on Ubuntu with FreeTDS, unixODBC, pyodbc (3.0.7) and django-pyodbc from here:
https://github.com/lionheart/django-pyodbc/
I can successfully run syncdb and South migrations. However, when I try to run the dbshell or loaddata, I get this error:
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect
I can connect from the command line using isql and tsql. Any ideas on what I'm missing?
So, I figured out the problem.
If you want to use dbshell or loaddata, you need to use a named DSN instead of just a host. I had this set up in /etc/freetds.conf, /etc/odbcinst.ini, and /etc/odbc.ini correctly, so tsql and isql were working.
I was using this default DATABASE in settings.py:
'ENGINE': 'django_pyodbc',
'NAME': 'db_name',
'USER': 'user_name',
'PASSWORD': 'pw',
'HOST': 'hostname.domain.com,1433',
'PORT': '1433',
'OPTIONS': {
'host_is_server': True,
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=7.2'
},
I had to change it to:
'ENGINE': 'django_pyodbc',
'NAME': 'db_name',
'USER': 'user_name',
'PASSWORD': 'pw',
'PORT': '1433',
'OPTIONS': {
'host_is_server': True,
'dsn': 'dsn_name',
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=7.2'
},
You will notice the first (broken) example uses a 'HOST', while the second (working) example uses a 'dsn' under 'OPTIONS."

Categories

Resources