how to use SQL connection string class in python - python

i want to create a class which maintain sql connection instead of creating sql connection everytime.
models.py
in models.py i declare sql connection in sqlconn method whenever i call this i want to establish sql connection in views.py
from django.db import models
import pyodbc
def sqlconn(request):
constr = "DRIVER={SQL Server};SERVER=localhose;DATABASE=testdb;UID=user;PWD=password"
conn = pyodbc.connect(constr)
return conn
Views.py
in this views.py i'm retrieving employee data from database instead of writing sqlconnection string in every views.py method. I want to call sqlconn method from models and i want to do further process like
getdata, insertdata, deletedata, updatedate
from django.shortcuts import render
from .models import sqlconn
import pyodbc
def getEmpData(request):
conn = sqlconn
cursor = conn.cursor()
#cursor.execute("select * from Employee")
cursor.execute("exec sp_demotable")
result = cursor.fetchall()
return render(request, 'getempdata.html', {'result':result})
i tried above code but its not working its showing error, any help thanks in advance
Note:
what is the best practices for python any articles are videos tutorials provide its great help for me, because i'm new to python but i've good knowledge in .NET Programming

I would suggest you to use SQLAchemy library as ORM for project.
https://docs.sqlalchemy.org/en/14/orm/tutorial.html

Related

How to pass 'using' DB to the django connection object

To query a specific database in django I can do:
Item.objects.using('specific_db').all()
Is there a way to do the same using a django connection? For example:
>>> from django.db import connection
>>> cursor=connection.using('specific_db').cursor()
If not, how could I get a cursor/connection for a specific DB without manually providing all the credentials?
According the the django documentation on Using raw SQL on multiple databases, you would use connections rather than connection:
from django.db import connections
cursor = connections['specific_db'].cursor()
cursor.execute("select * from item")
Use this code snippet for performing raw SQL queries
from django.db import connection
def connect_to_db():
with connection.cursor() as cursor:
cursor.execute("""SELECT * FROM Table""")
return dict(zip([col[0] for col in cursor.description], row)) for row /
in cursor.fetchall()

sqlalchemy is not returning updated row information

I have a web application that is on top of mysql. When I started it, I built the mysql database from scratch, and connected to it using pymysql.
Fast forward...
I've rewritten everything using sqlalchemy to connect to the db (non-declarative?) I can connect to the db, read the db, update, etc. I also use MySQLWorkbench to view the database in a graphic way.
My webapp has a few tables that will poll the database for changes, and update the table. For instance, I have a job queue which will update the percentage-done. Here's the kicker:
When I send a job through, I will watch the database in MySQLWorkbench. I can verify that the job is going through (I see the percentage climbing). This confirms that the webapp is writing to the database. But! On the other end, where the table is asking for the status, it is not recieving the updated information (I've done print outs in the models, as well as in the flask app, as well as the FE js).
What is going on here? Even though I can see it in MySQLWorkbench is the connection not 'releasing' or something? Here is some of my code:
#models.py
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker, Query
import os
import json
Base = automap_base()
engine = create_engine("mysql+pymysql://user:asdas758ef10d2364d54d7e8#localhost:{}/metadatacontroller".format(3306))
# reflect the tables
Base.prepare(engine, reflect=True)
EncodeQueue = Base.classes.encode_queue
db_session = scoped_session(sessionmaker(bind=engine))
class DataStore:
def __init__(self):
self.db = db_session()
#receive status
def queue_rendering_items(self):
query = self.db.query(EncodeQueue).filter(EncodeQueue.status.in_(['queue', 'rendering', 'rendering thumbnails'])).all()
return_query = []
for item in query:
row = {'artist_id': item.artist_id,
'art_id': item.art_id,
'status': item.status,
'progress': item.errors,
'artwork_title': item.artwork_title,
'artist_name': item.artist_name,
}
return_query.append(row)
return return_query
#update status
def update_render_progress(self, job_id, progress):
self.db.query(EncodeQueue).filter_by(job_id=job_id).update\({'errors': json.dumps(progress)})
self.db.commit()
(ignore the back slash in update_render_progress ... weird SO formatting)
This is how 'status' code is being called on a flask end point:
#app.route('/api/jobs', methods=["GET"])
def get_all_jobs():
db = models.DataStore()
all_art = db.queue_rendering_items()
print(all_art)
return jsonify({'data':all_art})
Lastly, the status is being updated by a different file, on a different machine:
import models
db = models.DataStore()
db.update_render_progress(job_id, {'percentage': percentage_complete, 'time_remain': render.render_estimated_seconds_remaining})
What the heck is happening?

SQL query using current_user doesn't filter results

The following query retrieves every reservation in the database, but I only want to get the reservations that the current user made. When I run the query in the mysql command line, it works. Why isn't this working in my app?
from flaskext.mysql import MySQL
from flask.ext.login import LoginManager
from flask_login import current_user
...
db = MySQL()
db.init_app(app)
...
cur = db.connect().cursor()
cur.execute("SELECT ReservationID FROM Reservation WHERE user_name=" + "'" + current_user + "';")
reservation_instance = cur.fetchall()
current_user is a User instance. You can't just add it to a string, you need to use it's username (assuming you called the attribute "username").
You are currently open to SQL injection attacks. Never try to insert parameters into a query string yourself. Always use parameterized queries, which lets the driver do the work of building, quote, and escape the query properly.
cur.execute('SELECT ReservationID FROM Reservation WHERE user_name=?', [current_user.username])
The placeholder character (I used ? in the example) depends on the driver. Consider using SQLAlchemy, which normalizes this sort of thing and is much more powerful. There is the Flask-SQLAlchemy to make integration with Flask easier as well.

Does anyone have an example to store crawl data from scrapy to MySQLdb using Peewee?

I'm new to Scrapy. I have Googled around and searched in Stack Overflow, but there's no exact things I want to do. I have been struggling with these for two days.
This is what I have gotten so far for pipelines.py. Would anyone point out what's wrong with it or show me some example code for connecting Scrapy to MySQLdb using Peewee?
from MySQLdb import *
from peewee import *
mysql_db = MySQLDatabase('nasdaq_db', user='root', passwd='')
class Quote(Model):
"""A base model that will use our MySQL database"""
time = CharField()
price = CharField()
volume = CharField()
class Meta:
database = mysql_db
db.connect()
Quote.create_table()
class RealTimeQuotePipeline(object):
def process_item(self, item, spider):
item = Quote(time=item['time'], price=item['price'], volume=['volume'])
item.save()
Run:
scrapy crawl nasdaq
Error Message:
peewee.OperationalError: (1049, "Unknown database 'nasdaq_db'")
If I change it to:
mysql_db = MySQLDatabase(db='nasdaq_db', user='root', passwd='')
There is another error message:
TypeError: __init__() takes at least 2 arguments (1 given)
You need to be sure the database nasdaq_db exists in your mysql instance. You can open up mysql and run:
create database nasdaq_db;
Then you should be able to connect. You had the correct syntax the first time around:
db = MySQLDatabase('nasdaq_db', user='root', passwd='')

Django UnicodeEncodeError when importing from db

I have a custom management command to import data from one db and create model instances from it. Basically it is:
class Command(BaseCommand):
def handle(self, **kwargs):
cursor = connections['db2'].cursor()
sql = 'SELECT * FROM table'
cursor.execute(sql)
for row in cursor.fetchall():
my_model = MyModel(*row)
my_model.save()
First I was importing from sqlite to sqlite and all went well. But when I switched to MySQL as my main db, I started getting UnicodeEncodeError, when calling my_model.save(). The caveat is that I have non-ascii symbols in db (namely Russian), but as I get, Django converts all strings to unicode. And yes, both dbs use utf-8.
It seems that bug is due to mezzanine app, I'm using
https://github.com/stephenmcd/mezzanine/issues/1132

Categories

Resources