I used pyodbc to access my MSSQL database.
When reading uniqueidentifier field from MSSQL, in my MacOS, I can print the correct value of udid field (e.g 4C444660-6003-13CE-CBD5-8478B3C9C984), however when I run the same code on Linux CentOS, i just see very strange string like "???E??6??????c", and the type of value is "buffer", not "str" as in MacOS.
Could you explain me why it is and how can i get correct value of uidi on linux? Thanks
In linux i use str(uuid.UUID(bytes_le=value)).upper() to get string like 4C444660-6003-13CE-CBD5-8478B3C9C984 of uniqueidentifier field
This is a few years old, but I've had to tackle this same problem recently. My solution was to simply CAST the unique identifier as a VARCHAR, which kept my Python code nice and tidy:
SELECT CAST(unique_id_column AS VARCHAR(36)) AS my_id FROM...
Then in Python, simply output row.my_id.
Related
So I just started working with sqlite in Python (and I am a beginner with Python as well), and I'm struggling with using the data from a Table.
Basically, I created my database but now I would like to do something with the data. I have tried just simply printing it using this function:
def Select():
surname = input("Whose birthdate would you like to know? (surname)\n")
print(c.execute("SELECT Birthday FROM People WHERE Surname=?", (surname,)))
but this is what gets printed when I call on the function.
<sqlite3.Cursor object at 0x0000020825545F10>
Anybody know what the fix for this is?
Thanks in advance!
You have to use fetchone function of the cursor to get one record or you can use fetchall to get all the row of the query.
Try using the method fetchone or fetch many.
print(c.fetchone())
or
print(c.fetchmany())
you can see the documentation here.
I connection to Mssql server on python 2.7 by pymssql.
Connection string is:
mssql+pymssql://user:pass#server:1433/DB
And Collation of server is Cyrillic_General_CI_AS.
When i try select from table with column of varchat it return string:
u'ÎÎÎ "ÒÎÐÏÅÄÀ"'
I did try to convert it with:
"ÎÎÎ "ÒÎÐÏÅÄÀ".decode('866')
And get output:
├О├О├О "├Т├О├Р├П├Е├Д├А"
But correct String in database is:
ООО "ТОРПЕДА"
It seems like each second simbol is correct.
How to get all varchar2 strings at correct encoding?
Thank you
Just found the answer after i did post the question:
u'ÎÎÎ "ÒÎÐÏÅÄÀ"'.encode('latin1').decode('1251')
May be there is a better solution, but after few days of looking i did't found.
I am using python ingress module for connectivity with vectorwise database.
For describe a table I am using the code below:
import ingresdbi
local_db = ingresdbi.connect(database ='x',uid ='y',driver ='z',pwd ='p')
local_db_cursor = local_db.cursor()
local_db_cursor.execute('help tran_applog ; ' )
I am getting this error :
Syntax error. Last symbol read was: 'help'."
Solutions will be appreciated. Thanks
The problem you've got is that 'help' isn't a real SQL statement that's understood by the DBMS server. It's really a terminal monitor command that gets converted into some queries against the system catalogs under the covers.
The alternative depends a little on what you're trying to get from the "describe table". The system catalogs relating to table and column information are iitables and iicolumns and you can do a select against them. Check the documentation or experiment.
Alternatively there appears to be a row descriptor you can get from ingresdbi, see the example here http://community.actian.com/wiki/Python_Row_Description
HTH
I believe you should do it like in any other shell script: echo "help tran_applog;" | sql mydatabase
Reason: "HELP" is not a standard SQL statement.
As suggested by PaulM, your best option to get metadata about tables is to query the system catalogs (iitables, iicolumns, iirelation, etc).
Start with something like:
SELECT C.column_name, C.column_datatype
FROM iitables T, iicolumns C
WHERE T.table_name = C.table_name
AND T.table_name = 'tran_applog';\g
So I have the following model
class Stock(models.Model):
name = models.CharField(max_length=50)
unit_measure = models.CharField(max_length=10)
unit_price = models.DecimalField(max_digits=10, decimal_places=2)
When I try to add an instance of that model in Django's admin site, it gives me the following error
(<class 'TypeError'>, TypeError('conversion from bytes to Decimal is not supported',))
Exception Location: /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/PyMySQL-0.5-py3.3.egg/pymysql/connections.py in defaulterrorhandler, line 209
But the data was inserted into the table successfully as I look up my database using phpmyadmin.
I am using Django1.5+Python3.3+MySQL5.5+PyMySQL
Anybody have ideas about what has gone wrong here?
After 11 months I hope the original poster found a workaround (better than switching to python 2).
The OP did not list his db connection string, but maybe he was using the "use_unicode=0" setting for his connection?
I was, and I hit the same type conversion error recently. Seems like it should be possible to convert from a byte string to a Decimal, maybe that is on someone's todo list :), but until then I can share what worked around the problem for me:
When connecting to mysql (through pymysql and python 3.4.1) set the charset=utf8 property (assuming you want that property, which you probably should) but do NOT set the use_unicode=0 property. I set that property on the advice of the current (0.9) sqlalchemy docs which said it would be "much faster." Faster but broken ain't an improvement :(. Maybe that advice was intended only for python2.x users? It's a bit confusing given how pymysql tries to be hot-swappable MySqlDB for python 3.x, but python's unicode & string handling has changed between 2.x and 3.x so...
Without diving deep into pymysql, I assume that with python3 "use_unicode" means that char fields are returned as python native (unicode) strings rather than "byte strings", with contents encoded as utf8. Set "use_unicode=0" and you get byte strings and thus the TypeError.
Anway, this works for me; hope this helps someone else who sees this error.
I had the same problem in SQLite 3, the solution I have found was mentioned in a Book (https://books.google.de/books?id=eLKdDwAAQBAJ&lpg=PA394&ots=xBGSOLY4Ue&dq=python%20sqlite%20byte%20to%20decimal&hl=de&pg=PA394#v=onepage&q&f=false)
def convert_decimal(bytes)
return decimal.Decimal(bytes.decode())
When using the python shell with the following:
>>> User.objects.get(pk=1)
I get the following error:
InvalidId: AutoField (default primary key) values must be strings
representing an ObjectId on MongoDB (got u'1' instead)
A possible solution to this problem, which did not work for me, may be found here: http://django-mongodb.org/troubleshooting.html
I'm wondering if anybody else has come across this problem and how were you able to fix it?
MongoDB doesn't use integer primary keys.