Is it possible to determine fields available in a table (MySQL DB) pragmatically at runtime using SQLAlchemy or any other python library ? Any help on this would be great.
Thanks.
Reflection could do this.
Reflect the database at once using sqlalchemy
meta = MetaData()
meta.reflect(bind=someengine)
users_table = meta.tables['users']
addresses_table = meta.tables['addresses']
# fields of address_table
fields = addresses_table.columns.keys()
See more information at http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#reflecting-database-objects
You can run the SHOW TABLE TABLENAME and get the columns of the tables.
This is part of the std DB api specification for python (pep 249), namely the description member on cursors, so you do not need SQLAlchemy.
for example for using http://www.pymysql.org/ , user is your table
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='mysql')
cur = conn.cursor()
cur.execute("SELECT * FROM user")
print cur.description
Related
In the website I'm making with Flask, I'm trying to "upgrade" my search bar by using the pg_trgm PostgreSQL extension to match words even after being spelled wrong. However, I'm having trouble figuring out how to use the extension's custom functions alongside SQLAlchemy.
Will I have to use raw SQL to properly perform the queries, or is there a way to do this more cleanly?
Assuming the extension is configured correctly, these functions should be accessible like any other database function. For example, here's how the similarity function might be called using SQLAlchemy core:
import sqlalchemy as sa
engine = sa.create_engine('postgresql+psycopg2:///test', echo=True, future=True)
metadata = sa.MetaData()
tbl = sa.Table('t70546926', metadata,
sa.Column('c1', sa.String, primary_key=True),
sa.Column('c2', sa.String))
tbl.drop(engine, checkfirst=True)
tbl.create(engine)
ins = sa.insert(tbl).values(c1='Alice', c2='Alison')
with engine.begin() as conn:
conn.execute(ins)
query = sa.select(sa.func.similarity(tbl.c.c1, tbl.c.c2).label('similarity_result'))
with engine.connect() as conn:
rows = conn.execute(query)
for row in rows:
print(row.similarity_result)
For Flask-SQLAlchemy, you might do something like this:
result = dbsession.query(sa.func.similarity(val1, val2)).all()
i've been trying to get some data from my db by using below code, but the code is not working. is there any mistake that i made in the code, if so how can i fix it.
NOTE: i took the below code from just a script not a django or flesk web app.
def db():
conn = psycopg2.connect(
"dbname=mydb user=postgres password=****** host=*.*.*.*")
cur = conn.cursor()
cur.execute("""SELECT * FROM MddPublisher""")
query_results = cur.fetchall()
print(query_results)
db()
ERROR: psycopg2.errors.UndefinedTable: relation "mddpublisher" does not exist LINE 1: SELECT * FROM MddPublisher
additionally,i want to show below code to prove that connection is ok. the problem is that i can't receive data from my db whenever i try to execute select command through python.
def print_tables():
conn = psycopg2.connect(
"dbname=mydb user=postgres password=***** host=*.*.*.*.*")
cur = conn.cursor()
cur.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cur.fetchall():
print(table)
print_tables()
OUTPUT:
('MddPublisher',)
This is probably an issue with case sensitivity. Postgresql names are usually normalized to lower case. However, when used inside double quotes, they keep their case. So, to access a table named MddPublisher you must write it like "MddPublisher".
All the gory details are in Section 4.1.1, Identifiers and Key Words in the Postgresql 14 docs.
I am trying to get table structure using python from sqlite3 (.db) database. for that I am using the below code, but it is giving syntax error any help?
import sqlite3
conn = sqlite3.connect('Db-IMDB.db')
cursor = conn.cursor()
cursor.execute('DESCRIBE Movie')
It's not MySQL, but sqlite3. In Python API you can retrieve information on table from this statement:
table_info = cursor.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall()
I am creating a Python script to interact with schema permissions (and relative tables) on Redshift. As suggested in some other StackOverflow posts I am using psycopg2 library.
When I try to execute some simple SELECT FROM queries I have no problems: I can execute and see results with no issues.
Problem comes when for example I try to create a new schema or granting / revoking permissions. This kind of queries don't look like to produce any effect.
Here I show a very simple example in which I attempt to create a new schema:
conn_string = "dbname='{}' port='{}' host='{}' user='{}' password='{}'".format(DB_NAME, DB_PORT, DB_HOST, DB_USER, DB_PWD)
con = psycopg2.connect(conn_string)
sql = "CREATE SCHEMA new_schema"
cur = con.cursor()
cur.execute(sql)
But when I look into the Redshift DB I don't see any new schema called new_schema. Same behaviour happens when I try to run some permission grant / revoke query.
Anyone knows what is going on?
You have to commit the transaction.
con = psycopg2.connect(conn_string)
sql = "CREATE SCHEMA new_schema"
cur = con.cursor()
cur.execute(sql)
con.commit()
I am testing Python and Mysql in that i am able to create and delete table's but i am unable to insert data in them.I searched stackoverflow and mostly they suggest to use
commit()
So i used it and even after i used the data is not inserted into the database.Please help me.
This is the code i use it creates the table but not inserting data
import MySQLdb
db = MySQLdb.connect("localhost","user","password")
cxn = MySQLdb.connect(db='test')
cursor = cxn.cursor()
cursor.execute("CREATE TABLE users(name VARCHAR(40),id VARCHAR(40))")
cursor.execute("INSERT INTO users(name,id) VALUES('John','1')")
db.commit()
print "Opertion completed successfully"
Are db and cxn connections to the same database?
You should establish your connection using following:
db = MySQLdb.connect(host="localhost",
db="test",
user="user",
passwd="password")
The cursor should then be derived from this connection via:
cursor = db.cursor()
I would hazard that your issue is coming from the ambiguity between db and cxn.