How to import psycopg2 into a new module im writing - python-3.x

So to simplify, I'm trying to write my own module (test.py) that looks as follows:
import psycopg2
get_data(xyz):
connection = psycopg2.connect(user="",
password="",
host="",
port="",
database="")
last_qry = """select * from xyz.abc"""
cursor = connection.cursor()
cursor.execute(last_qry)
last_data = cursor.fetchone()
cursor.close()
connection.close()
return last_data
in a different file I am running:
import test
get_data(xyz)
and I get the following error:
name 'psycopg2' is not defined
What am I doing wrong?

There are many bugs in these code snippets that you put here:
Your import should be like this:
from test import get_data
or in this way:
import test
test.get_data()
What is the use of xyz ? the second code snippet must return
NameError because xyz is not define;if you want to use it in last_qry you must have .format() for it.
What is the structure of directory that included the second file ?
and where is the first file?

Related

How to create a postgresql function in SQLAchemy DDL

everyone!
I wondered how I could create a function in PostgreSQL every time I create or replace a table on my database. I can't find an example that works for my case. So I tried to pass a string with the create command like this:
from sqlalchemy import create_engine
engine = create_engine('/path/to/db...')
conn = engine.connect()
func = 'CREATE OR REPLACE FUNCTION my_func() RETURN SETOF....'
conn.execute(func)
I got a Syntax error running the above code just before the "RETURN SETOF...". So, I try to do something like this:
from sqlalchemy import create_engine
engine = create_engine('/path/to/db...')
conn = engine.connect()
func = DDL('CREATE OR REPLACE FUNCTION my_func()'
'RETURN SETOF....')
func.execute_if(dialect='postgresql')
I know I'm missing something here, but I could not find out what's missing.
The syntax error was RETURN rather than RETURNS.

Run Python script at command line - cannot find '__main__' module error

This is the first time I have ever tried running a python script from the command line. I type: python C:\Users\Jonathan\OneDrive - Mazars in Oman\Trading\Systems\FibMatrix\Testing Trade Analysis\Python Codes\CreateDataTablePythonScriptv2.py -a='USDJPY'
I get this error: C:\Users\Jonathan\AppData\Local\Programs\Python\Python39\python.exe: can't find '__main__' module in 'C:\\Users\\Jonathan\\OneDrive'
The script trying to run is:
import sqlite3
import sys
PairName = sys.argv[1]
DTBLocation = 'C:/Users/Jonathan/OneDrive - Mazars in Oman/Trading/Systems/FibMatrix/Testing Trade Analysis//SQLite/Trade Analysis.db'
connection = sqlite3.connect(DTBLocation)
cursor = connection.cursor()
TableName = PairName+'_DATA'
cursor.execute("""CREATE TABLE IF NOT EXISTS {}
(
Date_Time INTEGER,
Open REAL,
Max_60m_Box REAL
)""".format(TableName))
connection.commit()
connection.close()
I have searched, but was not able to find an answer. I appreciate the advice!
can't find '__main__' module in 'C:\\Users\\Jonathan\\OneDrive'
is the hint here
your path contains spaces (and it tried to be parsed up to first space)
try enclosing whole path with quotes
python "C:\Users\Jonathan\OneDrive - Mazars in Oman\Trading\Systems\FibMatrix\Testing Trade Analysis\Python Codes\CreateDataTablePythonScriptv2.py" -a='USDJPY'

sqlite3.OperationalError: unable to open database file - error occurs after a certain while

Tooling:
Raspberry Pi 3B
Raspbian
BME280
Python3
Flask
Sqlite3
Error code:
Traceback (most recent call last):
File "BME280_DataCollector.py", line 65, in <module>
File "BME280_DataCollector.py", line 45, in logData
sqlite3.OperationalError: unable to open database file
Working on Raspbian and want to store my sensor data in sqlite3 database.
Somehow following error code occurs:
"sqlite3.OperationalError: unable to open database file".
Firstly, I thought that I requested the database file too quickly and changed the the measurement time to minutes, but the error is still reproducible.
I looked into /tmp by df /tmp. But this file system is used by 12 % and not overloaded.
Also, I tried to give the full path and also the database all write and read permissions via chmod, but also no differents. In addition, I put the full path to the code.
Furthermore, I tried to make try and exception approaches which also weren't fruitful.
Nevertheless, I wanted to know if this failure occures at a certain number of interactions with the database. I found out that it always stopped at the 1020th interaction.
I also tried to restart the python script with a shell script but it didn't work out due to lack of experience and knowledge.
Code:
from flask import Flask,render_template,url_for,request,redirect, make_response, send_file
import random
import json
from time import sleep
from random import random
from flask import Flask, render_template, make_response
import datetime
import sqlite3
import sys
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import io
import os
import smbus2
import bme280
## FUNCTIONS ##
# get data
def getBMEdata():
port = 1
adress = 0x77
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, adress)
bme_data = bme280.sample(bus, adress, calibration_params)
temp = '%.2f' % bme_data.temperature
hum = '%.2f' % bme_data.humidity
press = '%.2f' % bme_data.pressure
now = datetime.datetime.now() #get time
timeString = now.strftime('%d-%m-%Y %H:%M:%S') #write time to string
return temp, hum, press, timeString
# function to insert data on a table
def logData(temp, hum, press, timeString):
conn = sqlite3.connect(dbname)
curs = conn.cursor()
curs.execute("INSERT INTO BME280_data values((?), (?), (?), (?))", (timeString, temp, hum, press))
conn.commit()
conn.close()
# display data base
def displayData():
conn = sqlite3.connect(dbname)
curs = conn.cursor()
print("\nEntire database contents:\n")
for row in curs.execute("SELECT * FROM BME280_data"):
print(row)
conn.close()
## MAIN
if __name__ == '__main__':
count = 0
dbname = '/home/pi/projects/SmartPlanting/Sensors_Database/sensorsData.db'
sampleFreq = 60 #data collect every minute
while True:
temp, hum, press, timeString = getBMEdata() #get data
logData(temp, hum, press, timeString) #save data
sleep(sampleFreq) #wait
displayData() #show in terminal
#count = count+1
#print(count)
Maybe someone already solved this problem or can give me an alternative to sqlite3 which works with flask.
Suggestion: add a more complete exception handling routine, because your stacktrace could be more verbose.
But judging from your trace the offending line #45 could be this: conn.commit() (or the line above). Python is already helping you pinpoint the error. There is something wrong in function logData.
Could it be that you are feeding incorrect data to your table BME280_data ? To debug your application I would strongly recommend that you print log the data you are trying to insert (use the logging module to output to file and/or console). I don't know the structure of your table but some of your fields could have a definition (data type) that is not compatible with the data you are trying to insert. The fact that you are able to predictably reproduce the problem is quite telling and my hunch is that the data could be the cause.
To sum up: take good habits now and add at least basic exception handling.
A quality application should have exception handling and log errors so they can be reviewed and remediated by a human. This is even more important for unattended applications, because you are not in front of the console and you may not even have a chance to see problems that occur.
Here is one tutorial that may help: https://code.tutsplus.com/tutorials/error-handling-logging-in-python--cms-27932

Use a function from one script in another

I have a function that connects to a database thats predefined. The code looks like:
file1.py
def conn_db():
try:
cursor = pymysql.cursors.DictCursor
conn = pymysql.connect(host=settings.host,
user=settings.user,
password=settings.password,
db=settings.database,
port=settings.port,
cursorclass=cursor)
dbcursor = conn.cursor()
except pymysql.err.OperationalError as e:
print("Unable to make a connection to the mysql database. \n Error was:{}".format(e))
return conn, dbcursor
How can I use this function conn_db from file1.py in file2.py. And then call file2.py from executing python's intrepreter via python ?
Having a hard time even identifying something so basic, after several attempts.
Thank you.
You can use import file1 and then use file1.conn_db() to use the function.

NameError: name 'Database' is not defined

I keep getting the error "Database.db_search(search) NameError: name 'Database' is not defined".
If anybody could help me I would really appreciate it, I am also new to Stack Overflow so I apologize for any errors I may have made in regards to the overall layout of asking a question.
Thanks.
Database File:
from Staff import *
def db_search(self):
query = "SELECT * FROM customer_information WHERE Last_Name = %s"
cursor.execute(query, (last_name,))
for (Last_Name) in cursor:
print(Last_Name)
return last_name
Staff File:
from Database import *
def gett():
search = search_entry.get()
Database.db_search(search)`
By using
from Database import *
You've just imported all the contents of Database, so to get the thing to work you would now just need to call
db_search(search)
from module import * isn't really a recommended pattern, as you end up rather polluting your namespace (possibility of confusion between methods from different packages).
So if you change your import line to just:
import Database
then your code will work fine.

Resources