How to properly count rows from mysql using python? - python

I am trying to get rows quantity from query using python to mysql. I've been using rowcount but returned value is always 0.
def getLastData(md5TwitterDate):
conectar = connection()
try:
cursor = conectar.cursor()
query = "SELECT fecha,numeroreporte,internoid FROM jsywe_ncnsismos_sismosigp WHERE internoid = '%s' and published=1"
cursor.execute(query, md5TwitterDate)
lastEvent = cursor.fetchall()
rowsa = cursor.rowcount
except Error as ex:
print("Error to get data: ", ex)
finally:
if conectar.is_connected():
conectar.close()
#return False if rowsa> 0 else True
return rowsa
Also tried this way setting a variable to cursor.execute but in this case always get none
def getLastData(md5TwitterDate):
conectar = connection()
try:
cursor = conectar.cursor()
query = "SELECT fecha,numeroreporte,internoid FROM jsywe_ncnsismos_sismosigp WHERE internoid = '%s' and published=1"
rowsa = cursor.execute(query, md5TwitterDate)
lastEvent = cursor.fetchall()
except Error as ex:
print("Error to get data: ", ex)
finally:
if conectar.is_connected():
conectar.close()
#return False if filas > 0 else True
return rowsa
Tested query on database and it works, it returns 1 row

Related

Running multiples SQL queries in one statement (containing begin/end)

I am trying to populate an azure sql database using pyodbc.
I have around 14000item to push into the database per months, and running and executing each single upsert on it is really long.
I am so trying to run them in batch and push them all at once. Sadly, when doing this I am not getting anything in the database. Is there any way to make this works ?
i = 0
queryUpdate = ""
for values in valuesList:
if i == 100:
i = 0
queryUpdate = queryUpdate.replace('\'NULL\'', "NULL") #set null to real sql null
queryUpdate = queryUpdate.replace("True", "1") #replace true with 1 to match the bit type
queryUpdate = queryUpdate.replace("False", "0") #replace false with 0 to match the bit type
try:
cursor.execute(queryUpdate)
except pyodbc.Error as ex:
print(f"[x] Error when running the query:\n[x] {queryUpdate}\n[x] Exception: {ex}")
queryUpdate = ""
queryUpdate += f"""\n
if exists (select * from {tableName} with (updlock,serializable) where {colNames[keyIndex]} = {values[keyIndex]})
begin
update {tableName} set """
for i in range(len(colNames)):
queryUpdate += f"{colNames[i]} = '{values[i]}'"
if i != len(colNames) - 1:
queryUpdate += ","
queryUpdate += f"""
where {colNames[keyIndex]} = {values[keyIndex]}
end
else
begin
insert into {tableName} ({','.join(colNames)})
values {tuple(values)}
end;
"""
i+=1
try:
conn.commit()
except pyodbc.Error as ex:
print(f"[x] Error when commiting to the database.\n[x] Exception: {ex}")
else:
print("[+] Commit confirmed !")
cursor.close()
conn.close()
print("[+] Connection closed.")

Why does this SQL query fail?

I have a database class which abstracts some basic crud logic.
The issue lies in the fetch_single method:
The sql_insecure query works fine, and returns the expected results.
The sql_prepared query doesn't return any errors, but also doesn't return any results which match the parameters, when they clearly do exist within the db.
sql_prepared follows the same approach to prepared statements that the insert_single method implements, and this method also returns the expected results.
My question is; why is the sql_prepared query not returning any results?
import sqlite3
class Database:
def __init__(self, db: str):
try:
self.conn = sqlite3.connect(db)
self.cursor = self.conn.cursor()
except sqlite3.Error as e:
print(e)
self.__del__
def fetch_all(self, table: str):
try:
query = self.cursor.execute("SELECT * FROM ?", table)
rows = self.cursor.fetchall()
return rows
except sqlite3.Error as e:
print(e)
return False
def fetch_single(self, table: str, column_name: str, column_value):
sql_formatted_value = "'{value}'".format(value=column_value)
placeholder = ":{column_name}".format(column_name=column_name)
sql_insecrue = "SELECT * FROM %s WHERE %s=%s Limit 1" % (
table, column_name, sql_formatted_value)
sql_prepared = "SELECT * FROM %s WHERE %s=%s LIMIT 1" % (
table, column_name, placeholder)
# try:
# self.cursor.execute(sql_insecrue)
# rows = self.cursor.fetchall()
# return rows
# except sqlite3.Error as e:
# print(e)
# return False
try:
self.cursor.execute(sql_prepared, [sql_formatted_value, ])
rows = self.cursor.fetchall()
return rows
except sqlite3.Error as e:
print(e)
return False
def insert_single(self, table: str, data: list):
columns = ""
placeholders = ""
values = []
data_length = len(data)
for index, (key, value) in enumerate(data):
# we need to dynamically build some strings based on the data
# let's generate some placeholders to execute prepared statements
columns += "{column_name}".format(column_name=key)
placeholders += ":{column_name}".format(column_name=key)
# let's fill the insert values into a list to use with execute
values.append(value)
# only add a comma if there is another item to assess
if index < (data_length - 1):
columns += ', '
placeholders += ', '
sql = "INSERT INTO %s (%s) VALUES (%s)" % (
table, columns, placeholders)
try:
self.cursor.execute(sql, values)
self.conn.commit()
except sqlite3.Error as e:
print(e)
You cannot substitute table name using ? in prepared statements because it is not considered a query parameter.
I recommend doing something like this:
self.cursor.execute(f"DELETE FROM {table} WHERE id=?", [id])
In other words, use standard python format statements to specify your table name, but use prepared statement anchors like ? for any query parameters.
okay, i found the problem.
its was my sloppy sql syntax.
using backticks around the table and column name solved the issue.
def fetch_single(self, table: str, column_name: str, column_value):
sql_formatted_value = "'{value}'".format(value=column_value)
placeholder = ":{column_name}".format(column_name=column_name)
sql_insecure = "SELECT * FROM %s WHERE %s=%s" % (
table, column_name, sql_formatted_value)
sql_prepared = "SELECT * FROM `%s` WHERE `%s`=%s" % (
table, column_name, placeholder)
print(sql_insecure)
print(sql_prepared)
# try:
# self.cursor.execute(sql_insecure)
# row = self.cursor.fetchall()
# print(row)
# return row
# except sqlite3.Error as e:
# print(e)
# return False
try:
self.cursor.execute(sql_prepared,
[column_value, ])
row = self.cursor.fetchone()
return row
except sqlite3.Error as e:
print(e)
return False

I keep getting the error 'unread result found', no matter what I change

The code is given below which attempts to insert a value into the database and create one and insert if not available according to the usernames of the users.
I keep getting an error 'Unread result found' on the line highlighted with a comment below.
Thank you in advance!
def update_userDatabase(username,text_input):
available = False
mydb = mysql.connector.connect(host="localhost",user="root",passwd="1234",database="ChatBot")
mycursor = mydb.cursor()
now = datetime.datetime.utcnow()
mycursor.execute("SHOW TABLES")
result = mycursor.fetchone()
sg.popup()
if text_input == "":
for x in result:
sg.popup(x)
if x == username:
available = True
if available == True:
sql = """INSERT INTO {tab} (Date) VALUES (%s)""".format(tab=username)
val = (now.strftime('%Y-%m-%d %H:%M:%S'))
mycursor.execute(sql, val)
else:
sql = """CREATE TABLE {tab} (Date varchar(50),Questions varchar(20))""".format(tab=username)
mycursor.execute(sql)
sql = """INSERT INTO {tab} (Date) VALUES (%s)""".format(tab=username)
val = (now.strftime('%Y-%m-%d %H:%M:%S'))
*mycursor.execute(sql, val)* #THIS IS THE LINE WITH THE ERROR
elif username=="":
sql = "INSERT INTO %s (Questions) VALUES (%s)"
val = (text_input)
mycursor.execute(sql, val)

AttributeError: 'list' object has no attribute 'rowcount'

class code:
def rowcount(self):
return self._psql_cur.rowcount
And the code in the main program:
def sql_query(self, sql, *data, **kwdata):
"""
NOTE: This function returns a generator. So if you use it to do any kind of update to the dbms that doesn't
return anything, it won't be executed!
"""
self.last_select_id += 1
n_retrials = kwdata.get("___n_retrials", 0)
if n_retrials > 10:
raise OperationalError
assert not (len(data) > 0 and len(set(kwdata) - {"___n_retrials"}) > 0), \
"Pass either keyword-based data or comma-separated data."
time_start = time.time()
n_records_retrieved = 0
status = None
toclose = False
print "*********************inside db.py******************"
if self.logfile is not None:
self.logfile.write(">>> {} {} {} START SELECT\n{}\ndata={}\nkwdata={}\n\n".format(
self.cursor_id, self.last_select_id, time_start, sql, data, kwdata))
print "\n************* QUERY:\n", sql
print "\n***************************"
try:
if len(data) > 0:
print "\n**************printing data***********",data
print "\n******************printing sql**************************",sql
print "\n*******************************************************"
# self._psql_cur.execute(sql, data)
cur, toclose = self._execute_query(sql, data)
elif len(kwdata) > 0:
# self._psql_cur.execute(sql, kwdata)
cur, toclose = self._execute_query(sql, kwdata)
else:
cur, toclose = self._execute_query(sql, None)
print "################check###################"
n_records_reported = cur.rowcount
print "\n*************printing rowcount**********",n_records_reported
# Yield records
for record in cur:
n_records_retrieved += 1
if n_records_retrieved == n_records_reported:
status = "Finished"
yield record
following code conatains _execute_query:
def _execute_query(self, sql, args):
# sql = sql.lower().strip()
# print sql
sql_strip = sql.lower().strip()
print "-------4",args
# print self.dbname, sql_strip
if sql_strip.startswith("select ") or \
(sql_strip.startswith("with ")
# and "update " not in sql_strip and "insert " not in sql_strip
):
# Try to close previous named cursor
# if self._psql_cur is not None and not self._psql_cur.closed:
# try:
# self._psql_cur.close()
# except ProgrammingError:
# pass
# self._psql_cur.scroll(self._psql_cur.rowcount, mode="absolute")
# self._psql_cur.fetchone()
# self._psql_cur.fetchone()
# Create a new named cursor
self._psql_cur = self.connection.get_cursor()
print self.dbname, "NAMED", self._psql_cur
# Execute query
self._psql_cur.execute(sql, args)
rows = self._psql_cur.fetchall()
print "FETCHED RESULT: ", rows
print sql
return rows, True
#self._psql_cur.execute("""select * from recipes""")
#rows=self._psql_cur.fetchall()
#print "---------------5 ",rows[0]
#self._psql_cur.fetchall()
return self._psql_cur, True
else:
# if "insert " in sql or "update " in sql or "delete " in sql or "create" in sql:
# print self.dbname, "UNNAMED"
# In this case, do not use the named (server side) cursor
# self._psql_unnamed_cur = self._connection.get_cursor(named=False)
self._psql_unnamed_cur.execute(sql, args)
return self._psql_unnamed_cur, False
I can't figure out why I'm getting this error.
I am trying to get data from database here actually .This is a part of code available in the Github.(PACKAGE QUERY). This is the output I am getting:
Exception occured while executing query:
File "src/dbms/db.py", line 378, in sql_query
n_records_reported = cur.rowcount
AttributeError: 'list' object has no attribute 'rowcount'
Exception during experiment
'list' object has no attribute 'rowcount'
Please tell if you need more information about this doubt. :-)
Your _execute_query method returns a list when your query starts with select or with:
if sql_strip.startswith("select ") or \
(sql_strip.startswith("with ")
# and "update " not in sql_strip and "insert " not in sql_strip
):
# ...
rows = self._psql_cur.fetchall()
print "FETCHED RESULT: ", rows
print sql
return rows, True
rows is a list, not a cursor, so won't have the attribute. Either return the cursor there, or use len() to get a count of rows.

Pyscopg and Redshift, relation doesn't exists

I'm connecting to Redshift with Psycopg with the following piece of code
import psycopg2
con = psycopg2.connect(dbname='events', host=myhost,
port=my_post, user=my_user, password=my_pwd)
Then, check if a table called "event" exists and I do that like so
def table_exists(con, table_str):
#
exists = False
try:
cur = con.cursor()
cur.execute("select exists(select relname from pg_class where relname='" + table_str + "')")
exists = cur.fetchone()[0]
cur.close()
except psycopg2.Error as e:
print(e)
return exists
table_exists(con, "event")
Which returns True. However, when I try to read the table (for instance, below I try to return the column names) I get an error message saying that the "relation doesn't exists"
def get_table_col_names(con, table_str):
#
col_names = []
try:
cur = con.cursor()
cur.execute("select * from " + table_str + " LIMIT 0")
for desc in cur.description:
col_names.append(desc[0])
cur.close()
except psycopg2.Error as e:
print(e)
#
return col_names
get_table_col_names(con, "event")
Can you point me in the direction of what's happening?
for me its working fine
import psycopg2
rs_conn = psycopg2.connect(host='MYHOST',
user='NAME',
port='PORT NUM',
password='PASS',
dbname='DBNAME')
def get_table_col_names(con, table_str):
col_names = []
try:
cur = con.cursor()
cur.execute("select * from " + table_str + " LIMIT 0")
for desc in cur.description:
col_names.append(desc[0])
cur.close()
except psycopg2.Error as e:
print(e)
return col_names
columnnames = get_table_col_names(rs_conn, "TABLE NAME")
print(columnnames)
output:
[column names]

Categories

Resources