I am trying to SELECT the data in the column base on the given arg.
but an error appear Incorrect number of bindings supplied
class TheClass():
def __init__(self):
self.con = connect('mydb.db')
self.cur = self.con.cursor()
def getonedata(self, text):
self.variable = text
self.cur.execute("""SELECT username, age, password FROM usertbl WHERE username =?""",(self.variable))
self.result = self.cur.fetchone()
return self.result
def getoneresult(self, text):
for row in self.getonedata(text):
self.row1 = row[0]
self.row2 = row[1]
self.row3 = row[2]
print("{} {} {}".format(self.row1, self.row2, self.row3))
app = TheClass()
app.getonedata('IgiveUP')
You have to supply parameters as a tuple to the execute method, and in Python, a single-item tuple has to be specified with an additional comma:
self.cur.execute("""SELECT username, age, password FROM usertbl WHERE username =?""",(self.variable,))
Related
I have a class which contains functions that connect to snowflake (using snowflake connector) and perform data cleaning. My thought was to create multiple functions to do separate data cleaning.
I've defined one variable in a function called "work_data" called "self.calls" and I've defined that function as a pandas dataframe and I'd like to access that dataframe in another function.
Here's my current process:
I first initialized the variable in the init function and made it a blank list for now. After I've run the "work_data" function and try and run the "finalized_data" function..but I get the blank list instead of the panadas data frame.
Here's my code so far:
class SnowReader:
sql_pslink ='select * from xyz in xyz";'
sql_account= 'select * from xyz in xyz;'
sql_call = 'select * from xyz in xyz;'
sql_sales = 'select * from xyz in xyz;'
sql_address = 'select * from xyz in xyz;'
def __init__(self) -> None:
self.database = "xyz"
self.username = INTEGRATION_USER
self.password = SNOW_PASSWORD
self.account = "xyz"
self.warehouse = "xyz"
self.role = "xyz"
self.schema = "xyz"
self.conn = self._connect_snow()
self.calls = []
def _connect_snow(self):
try:
self.conn = snowflake.connector.connect(
user=self.username,
password=self.password,
account=self.account,
warehouse=self.warehouse,
database=self.database,
role=self.role,
schema=self.schema,
)
logger.info("You are connected to Snowflake")
except Exception as ex:
if ex.errno == 250001:
logger.error(
f"Invalid username/password, please re-enter username and password.."
)
def work_data(self):
if not self.conn:
self._connect_snow()
link = pd.read_sql(SnowReader.sql_pslink, self.conn)
account = pd.read_sql(SnowReader.sql_account, self.conn)
self.calls = pd.read_sql(SnowReader.sql_call, self.conn)
sales = pd.read_sql(SnowReader.sql_sales, self.conn)
Calls_merged = self.calls.groupby('xyz', as_index=False)['xyz'].count()
Account_step1 = Calls_merged.merge(account,left_on='xyz', right_on='xyz', how="left" )
Sales_merged = sales.groupby(['xyz'], as_index=False)['xyz'].count()
Account_final = Sales_merged.merge(Account_step1,left_on='xyz', right_on='xyz', how="left" )
Master_Address = pd.read_sql(SnowReader.sql_address, self.conn)
return Account_final, Master_Address, link, self.calls
def finalize_data(self):
return self.calls
a, b, c, d= SnowReader().work_data()
display(a,b,c, d)
testt = SnowReader().finalize_data()
testt
I want to create a class with methods, so that I do not have to create multiple functions.
Below is my code, I want to get a class with two methods. Method 1: SQL Query, method 2: sql insert.
Any tipp is greatly appreciated.
Stefan
def dbconnect():
dbconn = pymysql.connect(host='192.168.1.2', port=3307, user='username', passwd='password', db='dbname')
try:
cur = dbconn.cursor()
sqlQuery = "select * from data"
sqlQuerygetlast = "SELECT * FROM data ORDER BY id DESC LIMIT 1"
sqlQuerygetlast10 = "SELECT * FROM data ORDER BY id DESC LIMIT 10"
cur.execute(sqlQuerygetlast10)
rows = cur.fetchall()
for row in rows:
print(row)
except Exception as e:
print("Exeception occured:{}".format(e))
finally:
#dbconn.commit()
dbconn.close()
My objective is to call the methods from my code, i.e. query a select statement.
Thanks a lot
Stefan
I guess you mean that you don't want to create multiple connections?
Then you should implement it as a Context manager:
class DB:
def __init__(self):
self.dbconn = None
def get_last(self, n)
try:
cur = self.dbconn.cursor()
sqlQuerygetlast = "SELECT * FROM data ORDER BY id DESC LIMIT {}".format(n)
cur.execute(sqlQuerygetlast)
rows = cur.fetchall()
for row in rows:
print(row)
except Exception as e:
print("Exeception occured:{}".format(e))
finally:
# self.dbconn.commit()
def some_other_method(self):
self.dbconn.do_something()
def __enter__(self):
self.dbconn = pymysql.connect(
host='192.168.1.2',
port=3307,
user='username',
passwd='password',
db='dbname'
)
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
dbconn.close()
return True
and use it as follows:
with DB() as db:
db.get_last(1)
db.get_last(10)
db.some_other_method()
This will create only one instance of a database connection and close after it is finished.
Writing a class in Python is fairly simple.
Here's an example of how to write the classic Person class and how to use properties and methods.
Specifically, the presentation method is also making use of the name property.
From this example you can move on and build your Database class implementation quite easily:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def presentation(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.presentation()
Remember that in all the methods of a class you will have to specify the reserved keyword self as parameter (plus, clearly, any other parameter you might need).
I'm not sure how to describe the issue but I'll try it.
Background info
I have in my Django web application a function where the user can import other users. The user can via drag and drop import a .csv file which gets converted to a JSON 2D Array (with Papaparse JS)
In the view, I loop through the elements in the 2D array and create an "Importuser" which contains some properties like "firstname", "lastname", email and so on.
class Importuser:
firstname = None
lastname = None
email = None
import_errors = []
def __init__(self, fn, ln, e):
self.firstname = fn
self.lastname = ln
self.email = e
class Importerror:
message = None
type = None
def __init__(self, m, t):
self.message = m
self.type = t
In the for-loop, I also validate the email-address, so that there are no doubled users.
data = jsonpickle.decode(method.POST["users"])
users = []
for tempuser in data:
u = validate(Importuser(tempuser[0], tempuser[1], tempuser[2])
users.append(u)
In the validate function I check if there any user with the same email
def validate(user : Importuser):
user_from_db = User.objects.filter(email=user.email)
if user_from_db:
user.import_errors.append(Importerror("The user exists already!", "doubleuser"))
return user
Issue
After the for-loop finished all user have the same error but not when I print each user while doing the for-loop. The Importerror-Object in each user refers to the same memory location but in my test import should only have one user an error.
test.csv:
Dave,Somename,dave#example.com
Joe,Somename2,joe#example.com
Yannik,Somename3,yannik#example.com <<That's me (exsiting user)
What I'm doing wrong? can someone help me to understand why this happens?
You've defined import_errors as a class-level static, so it's shared between all instances of Importuser.
See: Static class variables in Python
For your particular problem, rewrite your classes as
class Importuser:
def __init__(self, firstname, lastname, email):
self.firstname = firstname
self.lastname = lastname
self.email = email
self.import_errors = []
class Importerror:
def __init__(self, message, type):
self.message = message
self.type = type
import_errors is a class-attribute of ImportUser. It should be a instance-attribute:
class Importuser:
def __init__(self, fn, ln, e):
self.firstname = fn
self.lastname = ln
self.email = e
self.import_errors = []
I'm create simple class for SQLite datadase and when I'm insert new row table doesn't change.
DB.py
import sqlite3
class DB:
def __init__(self, **kwargs):
self.db = sqlite3.connect('passwods.db')
self.c = self.db.cursor()
self.c.execute('CREATE TABLE IF NOT EXISTS passwords (name, value)')
def insert(self, alias, cipher):
column = (alias, cipher)
self.c.execute('INSERT INTO passwords (name, value) VALUES (?,?)', column)
self.db.commit()
def get(self, alias):
pk = (alias,)
self.c.execute('SELECT * FROM passwords WHERE name=?', pk)
def getAll(self):
self.c.execute('SELECT * FROM passwords')
Interactive shell
>>> from DB import DB
>>> db = DB()
>>> db.insert('firstName', 'firstValue')
>>> print(db.getAll())
None
>>>
Your method getAll has no return statement. If you add it, you can see that the table actually changes:
def getAll(self):
self.c.execute("SELECT * FROM passwords")
return self.c.fetchall()
I have 2 files which I am using python class methods to parse data from and create sql INSERT statements. In both of my files I have a list of gene names. The first file is a simple list of 20 gene names with a panel name that is always the same. The second file has more information about the genes, and there will be multiple entries of each gene name.
my code at the moment looks like:
class Gene(object):
def __init__(self, gene):
self.gene = gene
def make_gene_sql(self):
sql = """INSERT INTO gene ("""+"'"+self.gene+"', '"+panel+"')"
return sql
class Variants(object):
def __init__(self, gene, run_id, sample, variant):
self.run_id = run_id
self.sample = sample
self.gene = gene
self.variant = variant
def make_vsr_sql(self):
sql = "INSERT INTO variants_sample_run ("+ "'"+self.sample +"', '"+self.gene +"', '"+self.variant +"', '"+self.run_id+"')"
return sql
gene_file = open(gene_path, 'r+')
for line in gene_file:
gene_object = Gene(line.strip('\r\n'))
gene_sql = gene_object.make_gene_sql()
cursor.execute(gene_sql)
var_file = open(var_path, 'r+')
for line in var_file.readlines()[1:]:
item = line.split('\t')
sample = item[0]
run = item[1]
gene = item[2]
variant = item[3]
variants_object = Variants(gene, run, sample, variant)
sql = variants_object.make_var_sql()
cursor.execute(sql)
My question is: Is there a way I can make the Variants class inherit from the Gene class, and create a new function to check whether the gene variable in my variants_object is in the genes_object? I.e pass my variables through the gene class to make sure that the gene variable from my var_file is definitely in the gene file? Something like this (but actually works!)?
class Variants(Gene):
def __init__(self, gene, run_id, sample, variant):
self.run_id = run_id
self.sample = sample
self.gene = gene.gene
self.variant = variant
def check_gene(gene):
if self.gene not in gene.gene:
print('Gene not correct')
def make_vsr_sql(self):
sql = "INSERT INTO variants_sample_run ("+ "'"+self.sample +"', '"+self.gene +"', '"+self.variant +"', '"+self.run_id+"')"
return sql
I was thinking this could perhaps use a list created from the Gene class containing all of the genes, however I'm not sure whether this is the best way to do this?
I don't think inheritance can help you with this one. You can try to make a class variable for all Gene:
class Gene(object):
# The class variable that keeps track of all genes
genes = []
def __init__(self, gene):
Gene.genes.append(gene)
self.gene = gene
And your Variants class:
class Variants(Gene):
def __init__(self, gene, run_id, sample, variant):
self.run_id = run_id
self.sample = sample
self.gene = gene
self.variant = variant
def check_gene(gene):
if self.gene not in Gene.gene:
print('Gene not correct')
class Variants(Gene):
def __init__(self, gene, run_id, sample, variant):
self.run_id = run_id
self.sample = sample
self.gene = gene.gene
self.variant = variant
def check_gene(gene):
if self.gene not in gene.gene:
print('Gene not correct')