Here is my code snippet...
First I've called __execute_query function to insert some rows and then to select few rows..
However, if executed a select query the function returns None or Empty rows... However manual sql query returns few rows..
def __execute_query(self, query, parameters = [], set_row_id = False):
conn = MySQL.get_conn()
cursor = conn.cursor()
cursor.execute(query, parameters)
result = None
query_type = query[0:query.find(" ")]
query_type = query_type.lower()
if query_type in ('insert', 'update', 'delete'):
result = cursor.rowcount > 0
if result:
conn.commit()
if query_type == 'insert' and set_row_id == True:
"""
Update the object with the newly saved row ID and mark
the object as not new.
"""
self.__new_row = False
self.__row_id = conn.insert_id()
if self.__row_id == 0 or self.__row_id == "0":
self.__row_id = cursor.lastrowid
else:
result = cursor.fetchall()
cursor.close()
return result
Related
I try to mock my db but when I test it the result is None.
try:
con = psycopg2.connect(
host="yhvh",
database="python_db",
user="postgres",
password="pass",
)
except:
print("Unable to connect database")
# Open a cursor to perform database operation
cur = con.cursor()
def read(con):
"""
Read data in Database
"""
print("Read")
# execute the query
data ="SELECT id, name FROM employees"
cur.execute(
data
)
# fetchall - returns all entries
rows = cur.fetchall()
for r in rows:
print(f"id {r[0]} name {r[1]}")
this is the code for my testing
def test_read(self):
expected = [9, 'aaa']
with patch('psycopg2.connect') as mock_connect:
mock_con_cm = mock_connect.return_value
mock_con = mock_con_cm.__enter__.return_value
mock_cur = mock_con.cursor.return_value
mock_cur.fetchall.return_value = expected
result = db.read(mock_connect)
self.assertEqual(expected, result)
I get an assertionError: [9, 'aaa'] != None
How the result to have a value that would result is equal to expected ?
First you need to return the rows which contains the list of data from read function if not it will return None.
Then use assertListEqual(expected, result) to check the elements in the list.
Your final code will look like this.
def read(con):
"""
Read data in Database
"""
print("Read")
# execute the query
data ="SELECT id, name FROM employees"
cur.execute(
data
)
# fetchall - returns all entries
rows = cur.fetchall()
for r in rows:
print(f"id {r[0]} name {r[1]}")
return rows
And assertion should be,
self.assertListEqual(expected, result)
I have an sql table which i want to update based on a function
this is my code so far:
def read(conn):
cursor = conn.cursor()
#cursor2 = conn.cursor()
cursor.execute("SELECT All [id],[keyword],[result],[status],[createddate] FROM [Table1].[dbo].[aa]")
index = 1
cursor.execute("set nocount on")
for row in cursor:
s = row[1]
s = re.sub(r'[^\w\s]', '', s)
a=do_func(s)
if a:
cursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'True' where id ={}".format(index))
else:
cursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'False' where id ={}".format(index))
if index == 10:
break
index += 1
i get pyodbc.ProgrammingError: No results. Previous SQL was not a query.
I added "set nocount on" but didn't solve i also tried making a second cursor but also didn't solve problem
okay, see the code: you will need to split the cursor for select and cursor for update, you cannot use both at the same time. And after update, you will need to commit. Let me know if it works.
def read(conn):
selectcursor = conn.cursor()
updatecursor = conn.cursor()
selectcursor.execute("SELECT [id],[keyword],[result],[status],[createddate] FROM [Table1].[dbo].[aa]")
index = 1
result = selectcursor.fetchall()
for row in result:
s = row[1]
s = re.sub(r'[^\w\s]', '', s)
a=do_func(s)
if a:
updatecursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'True' where id ={}".format(index))
updatecursor.commit()
else:
updatecursor.execute("update [Table1].[dbo].[aa] set status = 'completed', result = 'False' where id ={}".format(index))
updatecursor.commit()
if index == 10:
break
index += 1
selectcursor.close()
updatecursor.close()
write python program to create a mysql table and insert data into this table,the program is as follows:
def pre_data_db_manage(type,data):
conn = pymysql.connect(host="localhost", port=3306, user="root", passwd="********", db="facebook_info",charset="utf8")
cur = conn.cursor()
if type == "pre_davi_group_members_data":
is_exist_table_sql = "SHOW TABLES LIKE 'fb_pre_davi_group_members_posts'"
if cur.execute(is_exist_table_sql) == 0:
create_table_sql = '''CREATE TABLE fb_pre_davi_group_members_posts (id bigint not null primary key auto_increment,userID bigint,userName varchar(128),userURL varchar(256),
postTime varchar(128),postText text,postTextLength int,likesCount int,sharesCount int,commentsCount int,postTextPolarity varchar(64),postTextSubjectivity varchar(64))'''
cur.execute(create_table_sql)
r = re.compile(r'^[a-zA-Z0-9]')
for item in data:
if "'" in item["PostText"]:
item["PostText"] = item["PostText"].replace("'"," ")
if "\\" in item["PostText"]:
item["PostText"] = item["PostText"].replace("\\","\\\\")
for i in item["PostText"]:
result = r.match(i)
if result == None:
print("in re")
item['PostText'] = item['PostText'].replace(i, ' ')
if "nan" in item["SharesCount"]:
item["SharesCount"] = 0
if "nan" in item["LikesCount"]:
item["LikesCount"] = 0
if "nan" in item["CommentsCount"]:
item["CommentsCount"] = 0
if "nan" in item["PostTextLength"]:
item["PostTextLength"] = 0
item["PostTextLength"] = int(item["PostTextLength"])
item["LikesCount"] = int(item["LikesCount"])
item["SharesCount"] = int(item["SharesCount"])
item["CommentsCount"] = int(item["CommentsCount"])
if type == "pre_davi_group_members_data":
insert_sql = '''INSERT INTO fb_pre_davi_group_members_posts (userID,userName,userURL,
postTime,postText,postTextLength,likesCount,sharesCount,commentsCount,postTextPolarity,postTextSubjectivity) VALUES
({0},"{1}",'{2}','{3}','{4}',{5},{6},{7},{8},{9},{10})'''.format(item["UserID"],item["UserName"],item["UserURL"],item["PostTime"],item["PostText"],item["PostTextLength"],item["LikesCount"],item["SharesCount"],item["CommentsCount"],item["PostTextPolarity"],item["PostTextSubjectivity"])
print(insert_sql)
try:
cur.execute(insert_sql)
except Exception as e:
print("insert error")
continue
cur.close()
conn.commit()
conn.close()
and write call statement as follows:
type = "pre_davi_group_members_data"
pre_data_db_manage(type, df_list)
however,when execute this program, found that no data have been inserted into table:fb_pre_davi_group_members_posts,
in the mysql order line, write:
select count(*) from fb_pre_davi_group_members_posts;
the result is 0
could you please tell me the reason and how to solve it
Following is the code, i get the result as empty array but cursor has rows shows rowcount > 1 :
``
def __init__(self,readLink):
if readLink==0:
self.linksToRead = 1000000000
else:
self.linksToRead = readLink
self.linkCount = 0
self.wordsList =[]
self.parsedLinks=[]
self.urlList =[]
self.connection = pymysql.connect(host='localhost',
user='root',
password='s3cr3tp#ssw0rd',
db='scrapper',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor, autocommit=True)
with self.connection.cursor() as cursor:
sql2 = "SELECT * FROM `linksparsed`"
try:
cursor.execute(sql2)
#self.connection.commit()
except Exception as ex:
print(ex)
result = cursor.fetchall()
cursor.rowcount #shows 3
totalLength = len(result) # shows 0
for row in result:
self.parsedLinks.append(row)
What is the rownumber after fetchall() ?
if it is 3, equals to rowcount, means have already fetched rows.
This is the source codes of fetchall, suppose return at "result self._rows[self.rownumber:]"
def fetchall(self):
"""Fetch all the rows"""
self._check_executed()
if self._rows is None:
return ()
if self.rownumber:
result = self._rows[self.rownumber:] <<-------
else:
result = self._rows
self.rownumber = len(self._rows)
return result
I have a small problem with this class which handle my DB. It still saying:
cursor.execute(sql)
ValueError: operation parameter must be str
I tried lots of things but nothing work as i want. I looked over https://docs.python.org/3.4/library/sqlite3.html and i'm sure i do the same things.
import sqlite3
class Database():
def __init__(self):
try:
self.db = sqlite3.connect('../database.sqlite')
self.cur = self.db.cursor()
self.cur.execute('pragma foreign_keys="1"')
except sqlite3.Error as e:
raise e
def select(self,sql):
cursor = self.db.cursor()
cursor.execute(sql)
records = cursor.fetchall()
cursor.close()
return records
def insert(self,sql):
cursor = self.db.cursor()
cursor.execute(sql)
newID = cursor.lastrowid
self.db.commit()
cursor.close()
return newID
def execute(self,sql):
""" execute any SQL statement but no return value given """
cursor = self.db.cursor()
cursor.execute(sql)
self.db.commit()
cursor.close()
if __name__ == '__main__':
db = Database()
#sql = "SELECT skuref, titre_prod FROM product"
t = ("888888",)
sql= "UPDATE product SET created = 1 WHERE skuref = ?", t
db.execute(sql)
If someone can help me it would be grateful.Later i wanted to pass something like this in the main program inside a for loop
lastpost = record[0]
if created = True
sql = "UPDATE product SET created = 1 WHERE skuref = ?",(lastpost,)
db.execute(sql)
sql is a tuple containing SQL statement and the parameters.
Change as following, so that sql and parameters are passed separately, instead of being passed as a tuple:
def execute(self, sql):
""" execute any SQL statement but no return value given """
cursor = self.db.cursor()
cursor.execute(*sql) # <------
self.db.commit()
cursor.close()
With your statement
sql = "UPDATE product SET created = 1 WHERE skuref = ?",(lastpost,)
you have created a tupel like
("UPDATE product SET created = 1 WHERE skuref = ?", (lastpost,))
You have to give the arguments as parameters to the execute() function.
Also your if statement is bad: no :, = instead of == and the whole check for True is no nesesary.
Try this:
lastpost = record[0]
if created:
sql = "UPDATE product SET created = 1 WHERE skuref = ?"
db.execute(sql, lastpost)