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

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()

Related

how to use SQL connection string class in 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

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

Why Doesn't Django Reset Sequences in SQLite3?

Why does Django allow you to reset the sequences (AutoID) fields on postgres and other DBMS's but not SQLite3?
Looking at the source code for the sql_flush method in django/db/backends/sqlite3/base.py, there is a comment that says:
Note: No requirement for reset of auto-incremented indices (cf. other sql_flush() implementations). Just return SQL at this point
I have a few tests where I load in fixture files that depend on absolute primary key ids. Because Django doesn't reset the auto id field for SQLite, these fixtures do not load correctly.
It appears that it is somewhat trivial to reset the auto id columns in sqlite: How can I reset a autoincrement sequence number in sqlite
You can monkey-patch sql_flush as follows to reset SQLite sequences:
from django.db.backends.sqlite3.operations import DatabaseOperations
from django.db import connection
def _monkey_patch_sqlite_sql_flush_with_sequence_reset():
original_sql_flush = DatabaseOperations.sql_flush
def sql_flush_with_sequence_reset(self, style, tables, sequences, allow_cascade=False):
sql_statement_list = original_sql_flush(self, style, tables, sequences, allow_cascade)
if tables:
# DELETE FROM sqlite_sequence WHERE name IN ($tables)
sql = '%s %s %s %s %s %s (%s);' % (
style.SQL_KEYWORD('DELETE'),
style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(self.quote_name('sqlite_sequence')),
style.SQL_KEYWORD('WHERE'),
style.SQL_FIELD(self.quote_name('name')),
style.SQL_KEYWORD('IN'),
', '.join(style.SQL_FIELD(f"'{table}'") for table in tables)
)
sql_statement_list.append(sql)
return sql_statement_list
DatabaseOperations.sql_flush = sql_flush_with_sequence_reset
You would use it as follows for example in a TransactionTestCase:
from django.test import TransactionTestCase
class TransactionTestCaseWithSQLiteSequenceReset(TransactionTestCase):
reset_sequences = True
#classmethod
def setUpClass(cls):
super().setUpClass()
if connection.vendor == 'sqlite':
_monkey_patch_sqlite_sql_flush_with_sequence_reset()
This assures that tests that depend on fixed primary keys work with both SQLite and other database backends like PostgreSQL. However, see Django documentation for caveats regarding reset_sequences. For one thing, it makes tests slow.
Maybe this snippet will help:
import os
from django.core.management import call_command
from django.db import connection
from django.utils.six import StringIO
def reset_sequences(app_name):
os.environ['DJANGO_COLORS'] = 'nocolor'
buf = StringIO()
call_command('sqlsequencereset', app_name, stdout=buf)
buf.seek(0)
sql = "".join(buf.readlines())
with connection.cursor() as cursor:
cursor.execute(sql)
print("Sequences for app '{}' reset".format(app_name))

Categories

Resources