ImportError: cannot import name JSONB, from sqlalchemy.dialects.postgresql - flask-sqlalchemy

I wrote a model which has code something like this.
But JsonB is not imported from postgres. I have no idea why its not happening. How do i make it work?
from sqlalchemy.dialects.postgresql import JSONB
class DataTable(Base):
sourceid = Column(Integer)
extraInfo = Column(JSONB)
Version of Alchemy:
SQLAlchemy==0.9.3
Version of Postgres:
postgresql-9.4
ImportError: cannot import name JSONB

Support for Postgres was introduced in SQL Alchemy 0.9.7. You can upgrade your SQL Alchemy to the new version using pip install SQLAlchemy --upgrade

Related

pandas to_sql with Exasol

When I use to_sql to upload dataframe to exasol and specify if_exists='replace', the default string data type is 'text', which is not supported by Exasol. I think Varchar is the right type. How could I make to_sql to create table with Varchar rather than Text?
I know it's not 100% what you are asking for but I would suggest to use the pyexasol package for communication between Pandas and Exasol. Deleting and following uploading then works like
import pyexasol
import _config as config
# Connect with compression enabled
C = pyexasol.connect(dsn=config.dsn, user=config.user,
password=config.password, schema=config.schema,
compression=True)
C.execute('TRUNCATE TABLE users_copy')
# Import from pandas DataFrame into Exasol table
C.import_from_pandas(pd, 'users_copy')
stmt = C.last_statement()
print(f'IMPORTED {stmt.rowcount()} rows in
{stmt.execution_time}s')
C.close()
Problems with varchar do not appear.

Python Error when querying a SQL query (not all arguments converted during string formatting)

I have the below python code that tries to pull some data from a SQL query. I however am getting an error
TypeError: not all arguments converted during string formatting
Given below is the code I am using
import pandas as pd
import psycopg2
from psycopg2 import sql
import xlsxwriter
def func(input):
db_details = conn.cursor() # set DB Cursor
db_details.execute(sql.SQL("""select name from store where name = (%s)"""), (input))
names = dwh_cursor.fetchall()
df = pd.DataFrame(names,columns=[desc[0] for desc in dwh_cursor.description])
Could anyone guide me where am I going wrong. Thanks
If i recall correctly you need to pass to sql query a name included in single quotes, so your query need to be ...where name = '{}' """.format(variablename)

django and row sql

What is the best way to import sql to python code:
I use django, but sometimes I write row sql, and sometimes this code is big. My question is how can I import examle.sql to my code.py
example.sql:
SELECT id FROM users_user
code.py:
row_sql = <this string>
user_ids = RowSqlManager(row_sql).execute()
if I name my example.sql with name example.py it is easy, but I want separate python code and sql. Is there to do it? Or it is better to rename example.sql to example.py

UUID to NUUID in Python

In my application, I get some values from MSSQL using PyMSSQL. Python interpret one of this values as UUID. I assigned this value to a variable called id. When I do
print (type(id),id)
I get
<class 'uuid.UUID'> cc26ce03-b6cb-4a90-9d0b-395c313fc968
Everything is as expected so far. Now, I need to make a query in MongoDb using this id. But the type of my field in MongoDb is ".NET UUID(Legacy)", which is NUUID. But I don't get any result when I query with
client.db.collectionname.find_one({"_id" : id})
This is because I need to convert UUID to NUUID.
Note: I also tried
client.db.collectionname.find_one({"_id" : NUUID("cc26ce03-b6cb-4a90-9d0b-395c313fc968")})
But it didn't work. Any ideas?
Assuming you are using PyMongo 3.x:
from bson.binary import CSHARP_LEGACY
from bson.codec_options import CodecOptions
options = CodecOptions(uuid_representation=CSHARP_LEGACY)
coll = client.db.get_collection('collectionname', options)
coll.find_one({"_id": id})
If instead you are using PyMongo 2.x
from bson.binary import CSHARP_LEGACY
coll = client.db.collectionname
coll.uuid_subtype = CSHARP_LEGACY
coll.find_one({"_id": id})
You have to tell PyMongo what format the UUID was originally stored in. There is also a JAVA_LEGACY representation.

What's OrientDB's SQL syntax for inserting a document with an embedded list of other documents?

Let's say we have in our OrientDB (1.7) schema a class Package, which should contain an embedded list of Versions. What's the SQL command to insert into OrientDB a document of the Package type with a list of Versions?
Assume that the current SQL statement to insert a Package, before introducing Versions, looks like this:
INSERT INTO Package (id) values ("apackage")
create class Version
create property Version.v string
create property Version.release string
create class Package
create property Package.id string
create property Package.versions embeddedlist Version
If your situation is the one described above I believe what you're looking for is:
insert into Package set id = "apackage", versions = [{"#type":"d","#class":"Version", "v":"1.0.1", "release":"monday"}]
# returns #13:0
If you want to add a new version:
update #13:0 add versions = [{"#type":"d","#class":"Version", "v":"1.0.2", "release":"tuesday"}]