how to run python/djanjo shell as a script - python-3.x

In a tutorial on django we create a simple table run migrations and then go into a shell with the command:
python manage.py shell
from there, in the shell we run the following:
from flights.models import Flight
f = Flight(origin="New York", destination="london", duration=415)
f.save()
I'm trying to figure out how to run these commands from a py file so I created test.py:
from flights.models import Flight
f=Flight(origin="New York",destination="London", duration=415)
f.save()
but get the error Models aren't loaded. How to resolve? I'm definitely a little confused. I am able to update the database from a web served page with the following in my views.py file:
from django.shortcuts import render
from flights.models import Flight
def index(request):
f=Flight(origin="New York",destination="London", duration=415)
f.save()
return render(request, "flights/index.html", {
})
What I am asking is how to update the database directly on the backend. Do I just use standard python sql commands? For instance:
import sqlite3
from sqlite3 import Error
import csv
def sql_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return conn
def sql_create(conn, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
def sql_insert(conn,sql,data,single):
cur=conn.cursor()
if single:
cur.execute(sql,data)
rowCount = cur.lastrowid
else:
cur.executemany(sql,data)
rowCount = cur.rowcount
conn.commit()
return(rowCount)
def sql_select(conn,sql,data):
cur = conn.cursor()
cur.execute(sql,data)
rows = cur.fetchall()
return rows
def sql_update(conn,sql,data):
cur = conn.cursor()
cur.execute(sql,data)
conn.commit()
def sql_delete(conn,sql,mydata):
print(mydata)
cur = conn.cursor()
cur.execute(sql,mydata)
conn.commit()
def main():
insert = False
db_file = r"/home/saltydog/Database/crud.db"
# create a database connection
conn = sql_connection(db_file)
# create tables
sql_create_price_table = """ CREATE TABLE IF NOT EXISTS prices (
ticker text NOT NULL,
ymd integer,
price real,
PRIMARY KEY(ticker,ymd)
); """
sql_create_price2_table = """ CREATE TABLE IF NOT EXISTS prices2 (
ticker text NOT NULL,
ymd integer,
price real,
PRIMARY KEY(ticker,ymd)
); """
if conn is not None:
# create projects table
sql_create(conn, sql_create_price_table)
sql_create(conn, sql_create_price2_table)
conn.commit()
else:
print("Error! cannot create the database connection.")
if(insert):
sql_insert_record = """insert into prices(ticker,ymd,price)
values(?, ?, ?)"""
cnt = 0
with open('ibm.txt') as f:
reader = csv.reader(f)
for row in reader:
ticker = row[0]
ymd = row[1]
price = row[2]
data = (ticker, ymd, price)
if cnt != 0:
rowid = sql_insert(conn,sql_insert_record,data,True)
print(rowid)
cnt+=1
sql_insert_records = """insert into prices2(ticker,ymd,price)
values(?, ?, ?)"""
data=[]
cnt=0
with open('ibm.txt') as f:
reader = csv.reader(f)
for row in reader:
ticker = row[0]
ymd = row[1]
price = row[2]
if cnt != 0:
data.append((ticker, ymd, price))
cnt+=1
rowid = sql_insert(conn,sql_insert_records,data,False)
print(rowid)
select_records = """select ticker,ymd,price from prices
where ticker = ?
group by price
order by price"""
data=('IBM', )
rows = sql_select(conn,select_records,data)
for row in rows:
print(row)
select_records = """select ticker,ymd,price from prices
where ticker = ?
and price > ?"""
data=('IBM',100.0)
rows = sql_select(conn,select_records,data)
for row in rows:
print(row)
select_records = """select ticker,ymd,price from prices
where ymd = ?"""
data=(19990527, )
rows = sql_select(conn,select_records,data)
for row in rows:
print(row)
sql_update_records = """update prices
set price = ?
where ymd = ?"""
data = (200.00,19990527)
sql_update(conn,sql_update_records,data)
sql_delete_record = """delete from prices where ymd = ?"""
mydata = (19990528, )
sql_delete(conn,sql_delete_record,mydata)
if __name__ == '__main__':
main()

Related

How to do validation in python?

I am making a gui for employee management system using python tkinter and sqlite3.
In this gui user can add, view, delete amd update employee info.
def save():
con = None
try:
con = connect("pro.db")
cursor = con.cursor()
sql = "insert into Employee values('%d', '%s', '%f')"
id = int(aw_ent_id.get())
name = aw_ent_name.get()
lenstring = False
while not lenstring:
if len(name) >= 2:
lenstring = True
else:
showerror("error","Enter atleast 2 letters")
break
salary = float(aw_ent_salary.get())
cursor.execute(sql%(id, name, salary))
con.commit()
showinfo("success", "record added")
aw_ent_id.delete(0, END)
aw_ent_name.delete(0, END)
aw_ent_salary.delete(0, END)
aw_ent_id.focus()
except Exception as e:
con.rollback()
showerror("issue", e)
finally:
if con is not None:
con.close()
the code is running but i am getting some errors in validating name and salary.
for name i have done validating but its not working. I am getting an error
the data is getting saved even after getting error.
What should i do to make it right?
It is better to:
validate the inputs before saving to database
raise exception if len(name) is less than 2 instead of using while loop checking (actually the while loop is meaningless)
use placeholders to avoid SQL injection
Below is updated save():
# avoid using wildcard import
import tkinter as tk
from tkinter.messagebox import showinfo, showerror
import sqlite3
...
def save():
con = None
try:
# validate inputs
# use emp_id instead of id because id is built-in function
emp_id = int(aw_ent_id.get().strip()) # raise ValueError if not a valid integer
name = aw_ent_name.get().strip()
if len(name) < 2:
raise Exception('Name too short, at least 2 letters')
salary = float(aw_ent_salary.get().strip()) # raise ValueError if not a valid float number
# validations on inputs are passed, try saving to database
sql = 'insert into Employee values (?, ?, ?)' # use placeholders to avoid SQL injection
con = sqlite3.connect('pro.db')
cursor = con.cursor()
cursor.execute(sql, (emp_id, name, salary))
con.commit()
showinfo('Success', 'Employee added')
aw_ent_id.delete(0, tk.END)
aw_ent_name.delete(0, tk.END)
aw_ent_salary.delete(0, tk.END)
aw_ent_id.focus_set()
except Exception as e:
if con:
con.rollback()
showerror("Error", e)
finally:
if con:
con.close()
...

Python .execute() not executing Postgresql query

I am trying to update a database from a csv file. I am able to connect to the database, run through the lines that are in the csv, compose the desired SQL line, but for some reason it never hits the Postgresql DB. Console and pycharm show no errors.
Here is the .py
import csv
import psycopg2
filename = 'C:\dev\student_feedback\student_feedback\student_feedback.csv'
fields = []
rows = []
host='localhost'
port=5432
database='ipfeedback'
user='postgres'
password='#########'
def import_student_survey_csv():
print('Maybe Updating surveys...')
def doQuery(connection):
cur = connection.cursor()
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
fields = next(csvreader)
for row in csvreader:
rows.append(row)
nrows = rows.pop()
qry = (f"INSERT INTO ipfeedbackdb_studentsurvey ({fields[0]},{fields[1]},{fields[2]},{fields[3]},{fields[4]},{fields[5]},{fields[6]},{fields[7]},{fields[8]},{fields[9]},{fields[10]},{fields[11]}) VALUES ({nrows[0]},{nrows[1]},{nrows[2]},{nrows[3]},{nrows[4]},{nrows[5]},{nrows[6]},{nrows[7]},{nrows[8]},'{nrows[9]}','{nrows[10]}','{nrows[11]}');")
print('QRY', qry)
cur.execute(qry)
csvfile.close()
myConnection = psycopg2.connect(host=host, port=port, user=user, password=password, dbname=database)
doQuery(myConnection)
myConnection.close()
import_student_survey_csv()
the .csv
added,spid,q1,q2,q3,q4,q5,q6,q7,best_part,improve,comments
21021,0,1,1,1,1,1,1,1,"qwe","qwe","qwe"
21021,0,1,1,1,1,1,1,1,"asd","asd","asd"
21021,0,1,1,1,1,1,1,1,"x","x","x"
21021,12345,1,1,1,1,1,1,1,"x","x","x"
the table data
the db structure

try to print some table is sqlite3 using python

hi I am a new in python i tried to create a new class that handle with sqlite3 in my read method i try to print some var but is does not print anything can you help me?
here is the code can you fix it and tell me what the problem is
class SQLite_class() :
file_name=''
table_sql_name =''
# con = sqlite3.connect(self.c)
def creat_file_name(self):
self.file_name = input("enter the SQL fille name !")
self.file_name = self.file_name+'.sqlite'
return self.file_name
def conncection(self):
conn = sqlite3.connect(self.file_name)
return conn
def creat_cursor(self):
conn = self.conncection()
cur = conn.cursor()
return cur
def del_table(self):
cur = self.creat_cursor()
cur.execute('DROP TABLE IF EXISTS '+self.table_sql)
def creat_table(self):
cur = self.creat_cursor()
#adding a new option (name of table of more we need to add some {} and use the format function)
cur.execute( '''CREATE TABLE IF NOT EXISTS {} (data TEXT, number INTEGER)'''.format(self.table_sql_name))
self.commiting()
def insrt(self):
cur = self.creat_cursor()
cur.execute('''INSERT INTO {} (data, number) VALUES (?, ?)'''.format(self.table_sql_name) ,('Thunderstruck', 20))
def close(self):
conn = self.conncection()
conn.close()
def commiting(self):
conn = self.conncection()
conn.commit()
def read(self):
cur = self.creat_cursor()
cur.execute('''SELECT data, number FROM {}'''.format(self.table_sql_name))
for row in cur: print(row)
test = SQLite_class()
test.creat_file_name()
test.table_sql_name = 'Track'
test.creat_table()
test.insrt()
test.commiting()
test.read()
test.commiting()
test.close()
This comment # con = sqlite3.connect(self.c) indicates you know what has to be done. I suspect when that didn't work, you went down the rabbit hole of creating connections everywhere. Program should make one db connection. Every time it makes a connection, it loses what came before.
It could declare conn = '', then call conncection() once (and remove it from the other methods; they'll probably need to take conn as an argument). It'll take some try, try again, but it will get you to a better place.
And don't forget to fetch the rows once the SELECT is executed.

Rendering multiple methods to multiple data table projected in single html under single flask routing

For the url (datatab), how may I render all different queries for different methods to multiple DataTables in single HTML page?
Currently only the first method is rendering data from db to table1 and other methods aren't working.
from flask import Flask, render_template, request
app = Flask(__name__)
app.debug = True
def connectToDB():
import psycopg2
connectionString = psycopg2.connect(host='127.0.0.1', user='null',
password='null', dbname='postgres')
try:
return connectionString
except:
print("data khuje pachchi na ")
#app.route('/')
def index():
return render_template('index.html')
#app.route('/datatab)
def doQuery():
conn = connectToDB()
cur1 = conn.cursor()
cur1.execute(
'select datid,datname,pid,usesysid,usename from pg_stat_activity;')
results1 = cur1.fetchall()
return render_template('datatable.html', doQuery=results1)
#app.route('/datatab')
def doQuery1():
conn = connectToDB()
cur2 = conn.cursor()
cur2.execute(
'select datid,datname,pid,usesysid,usename from pg_stat_activity;')
results2 = cur2.fetchall()
return render_template('datatable.html', doQuery1=results2)
def doQuery2():
conn = connectToDB()
cur3 = conn.cursor()
cur3.execute(
'select relid,indexrelid,schemaname,relname,indexrelname from pg_statio_all_indexes;')
results3 = cur3.fetchall()
return render_template('datatable.html', doQuery2=results3)
def doQuery3():
conn = connectToDB()
cur4 = conn.cursor()
cur4.execute(
'select datid,datname,numbackends,xact_commit,xact_rollback from pg_stat_database;')
results4 = cur4.fetchall()
return render_template('datatable.html', doQuery3=results4)
if __name__ == '__main__':
app.run()
This might help you.
#app.route('/datatab')
def doQuery():
conn = connectToDB()
cur1 = conn.cursor()
cur1.execute('select datid,datname,pid,usesysid,usename from pg_stat_activity;')
results1 = cur1.fetchall()
cur2 = conn.cursor()
cur2.execute('select datid,datname,pid,usesysid,usename from pg_stat_activity;')
results2 = cur2.fetchall()
cur3 = conn.cursor()
cur3.execute('select relid,indexrelid,schemaname,relname,indexrelname from pg_statio_all_indexes;')
results3 =cur3.fetchall()
cur4 = conn.cursor()
cur4.execute('select datid,datname,numbackends,xact_commit,xact_rollback from pg_stat_database;')
results4 = cur4.fetchall()
return render_template('datatable.html', doQuery=results1, doQuery1=results2, doQuery2=results3, doQuery3=results4)

Tweepy to sqlite3

I'm trying to use Twitter's Streaming API to save tweets to a database using sqlite3. The problem is my database comes back empty. I'm using DB Browser for SQLIte to check the schema and the table and columns are there but no values for any column. Any thoughts?
#create sql table
conn = sqlite3.connect('twitter_one5.db')
c = conn.cursor()
c.execute('''CREATE TABLE tweets
(coordinates integer,
place text)''')
conn.commit()
conn.close()
# open connection
conn = sqlite3.connect('twitter_one5.db')
c = conn.cursor()
Create a class to handle inserting tweet info into database:
class Tweet():
# data from tweet
def __init__(self, coordinates, place):
self.coordinates = coordinates
self.place = place
# insert data from tweet into DB
def insertTweet(self):
c.execute("INSERT INTO tweets (coordinates, place) VALUES(?, ?)",
(self.coordinates, self.place))
conn.commit()
Create a tweet listener using the Streaming API:
class listener(tweepy.StreamListener):
def on_status(self, status):
tweet_data = Tweet(status.coordinates, status.place)
tweet_data.insertTweet()
print('inserted')
Create instance of listener and filter for keywords:
l = listener()
stream = tweepy.Stream(auth, l)
stream.filter(track=['twitter'])

Resources