I'm a newbie to Python. I have the code below which basically extracts some 5 rows from a MySql table.
if keyword.lower() == 'last5':
cursor.execute('SELECT income_Ref, income_Amount FROM model_income WHERE company_MobileNumber = %s ORDER BY income_Datetime DESC LIMIT 5', [sender])
result = cursor.fetchall()
result = [list(row) for row in result]
self.respond(result)
return ()
The self.respond() kinda works like print() but it sends a text message instead.I am having trouble formatting the output. What I get from the code above is a list of lists which looks likes this:
[[u'2014-11-06fd753b-inc', u'50.0'], [u'2014-11-067d724b-inc', u'50.0'], [u'2014-11-067557d6-inc', u'50.0']]
I really wish I new how to make it look like this:
Ref:2014-11-06fd753b-inc Amount:50.0
Ref:2014-11-067d724b-inc Amount:50.0
Ref:2014-11-067557d6-inc Amount:50.0
That includes prefixing 'Ref' and 'Amount' before each respective field. I swear I have searched high and low on how to do that for a week now but I'm failing to crack it.
You can use:
self.respond("\n".join("Ref:{} Amount:{}".format(ref, amt) for ref, amt in result))
Not really beginner material, but...
Joining a string (created by formatting "Ref:{} Amount:{}") with "\n" using a generator
if keyword.lower() == 'last5':
cursor.execute('SELECT income_Ref, income_Amount FROM model_income WHERE company_MobileNumber = %s ORDER BY income_Datetime DESC LIMIT 5', [sender])
result = cursor.fetchall()
result = [list(row) for row in result]
self.respond("\n".join("Ref:{} Amount:{}".format(ref, amt) for ref, amt in result))
return ()
I left the list comprehension result = [list(row) for row in result] because I don't know what fetchall() actually returns.
Output:
Ref:2014-11-06fd753b-inc Amount:50.0
Ref:2014-11-067d724b-inc Amount:50.0
Ref:2014-11-067557d6-inc Amount:50.0
["Ref:" + i[0][2:-1] + " Amount: " + i[1][2:-1] + "\n" for i in result]
Related
I'm trying to search a database, by iterating through a list of search values. I'm almost there as this works for integers but not strings. The code below won't work, but if I replace the list values with numbers it does:
mycursor = mydb.cursor()
lst = []
select_query = "SELECT * FROM fruit WHERE content LIKE "
ids = ["apple", "pear"]
for x in ids:
var = select_query + str(x)
mycursor.execute(var)
lst.extend(mycursor.fetchall())
print(lst)
It's because you have to enclose strings in quotation marks in SQL. So for example
SELECT * FROM fruit WHERE content LIKE 'pears'
will work, and it will only work with the single quotations around "pears". Even better, type conversion can (and should) be done automatically with psycopg2:
select_query = "SELECT * FROM fruit WHERE content LIKE %s"
...
mycursor.execute(select_query, (x,))
https://www.psycopg.org/docs/usage.html#strings-adaptation
Hello and thank you for taking the time to read this. I want to extract large amounts of text from one database to another. The problem is that when I read the text from the database it contains a lot of "\N" "\n". I would really appreciate it if someone could point me in the right direction or tell what is going on here exactly and why the command fetchall is behaving like this...
Thank you in advance!
Example of the text in the TABLE:ourdata (I want to read from):
Example of text in the TABLE product_d I am writing to:
This is the code I am using:
import sqlite3
database_path = "E:/Thesis stuff/Python/database/"
conn = sqlite3.connect(database_path + 'test2.db')
c = conn.cursor()
current_number = 0
# creates the table to which the descriptions go
c.execute("CREATE TABLE IF NOT EXISTS product_d (product_description TEXT)")
conn.commit()
c.execute("SELECT description FROM ourdata")
text_to_format = c.fetchall()
print(text_to_format[0])
text_list = []
# make a list of the descriptions
for text in text_to_format:
text = str(text)
text_list.append(text)
# put all the elements of the list into the new table
for item in text_list:
c.execute("INSERT INTO product_d (product_description) VALUES (?)", (text_list[current_number],))
print("at number " + str(current_number))
current_number += 1
conn.commit()
c.close()
conn.close()
The giveaway is the parentheses around the string in the second example. fetchall() returns a list of tuples, and in your "make a list of the descriptions" block, you're explicitly converting those tuples to strings. Instead, what you want to do is simply grab the first (and only, in this case) element of the tuple. It should be as simple as changing this line:
text = str(text)
to this:
text = text[0]
Based on the question "psycopg2: insert multiple rows with one query" (psycopg2: insert multiple rows with one query)
From JS, I receive the following on serverside: "2,3,4..."
Then, on server side(python 3.6):
list_urlquery = urlquery_partition.strip().split(",")
for i in range(len(list_urlquery)):
list_urlquery[i] = "(" + str(list_urlquery[i]).strip() + ", '" + str(
file.filename).strip() + "," + str(PATH_ID).strip() + "')"
insert_query = 'INSERT INTO tbl_ma (theid, thefilename, thepathid) VALUES %s'
psycopg2.extras.execute_values(cursor, insert_query, list_urlquery, template=None, page_size=100)
print (list_urlquery)
Output on Console: ["(2, 'Screenshot from 2018-05-29 07-13-47.png,1')", "(3, 'Screenshot from 2018-05-29 07-13-47.png,1')", "(4, 'Screenshot from 2018-05-29 07-13-47.png,1')"]
Error: INSERT has more expressions than target columns
I request you to guide me on this. How can I create a proper list/tuple and insert the data into database?
Can you try it like this:
list_urlquery[i] = """({}, '{}', '{}')""".format(str(list_urlquery[i]).strip(), str(file.filename).strip(), str(PATH_ID).strip())
I am assuming only theid column is integer.
If the the pathid is also int (numeric), then try below one
list_urlquery[i] = """({}, '{}', {})""".format(str(list_urlquery[i]).strip(), str(file.filename).strip(), str(PATH_ID).strip())
As per the doc (http://initd.org/psycopg/docs/extras.html#fast-exec) it says, "argslist – sequence of sequences..."
Therefore, sequence of sequences == list of tuples
# creating list of tuples (sequence of sequences).
list_urlquery = urlquery_partition.strip().split(",") # list for "theid"
lst_filename = [] # list for "thefilename"
lst_pathid = [] #list for "thepathid"
lst_tpl_insertdata = [] # creating "List of Tuples == Sequence of sequences".
for i in range(len(list_urlquery)):
lst_filename.append(str(file.filename).strip())
lst_pathid.append(PATH_ID)
#combine all list using "zip()" for creating "List of Tuples == Sequence of sequences".
lst_tpl_insertdata = list(zip(list_urlquery,lst_filename,lst_pathid))
insert_query = 'INSERT INTO tbl_ma (theid, thefilename, thepathid) VALUES %s'
psycopg2.extras.execute_values(cursor, insert_query, lst_tpl_insertdata, template=None, page_size=100)
connection.commit()
I have a list in Python i.e:
['E/123', 'E/145']
I want to add this to a SQL statement that I made:
WHERE GrantInformation.GrantRefNumber LIKE 'E/123'
OR GrantInformation.GrantRefNumber LIKE 'E/145'
The code I have is:
items = []
i = 0
while 1:
i += 1
item = input('Enter item %d: '%i)
if item == '':
break
items.append(item)
print(items)
refList = " OR GrantInformation.GrantRefNumber LIKE ".join(items)
The problem is, when I insert that String into my SQL it is a String so it is looking for WHERE GrantInformation.GrantRefNumber Like 'ES/P004355/1 OR GrantInformation.GrantRefNumber LIKE ES/P001452/1'
Which obviously returns nothing as 'ES/P004355/1 OR GrantInformation.GrantRefNumber LIKE ES/P001452/1' does not appear in the field.
How do i do it so the ' GrantInformation.GrantRefNumber LIKE ' is not a String?
Thank you
The preferred way to do this, is to use a ORM like SQLAlchemy, which
does the query construction for you and you dont have to make the string concentrations yourself.
join(), adds the string between all the items in the array, that is passed as argument. You would need to add the condition into the array as well:
>>> items = ['A', 'B']
>>> " OR ".join(["GrantInformation.GrantRefNumber LIKE '%s'" % num for num in items])
"GrantInformation.GrantRefNumber LIKE 'A' OR GrantInformation.GrantRefNumber LIKE 'B'"
EDIT:
I have somewhat distilled the question.
mongo_documents = mongo_collection.find({"medicalObjectId": "269"})
print "\n\n"
for this_document in mongo_documents:
print this_document
print "-------------------------"
pqr = 269
mongo_documents2 = mongo_collection.find({"medicalObjectId": pqr})
print "\n\n"
for this_document2 in mongo_documents2:
print this_document2
My problem is that the first code chunk where I use the number as the key in the query, works. But the second chunk where I use the variable, i get no output.
I am a beginner at python and pymongo, so please bear with me.
I have a list as;
row = [1, 2, ...., 100]
I want to query a mongodb collection for each entry in my list.
The collection has the format:
collection = {'pk', 'attribute1', 'attribute2', 'attribute3'}
I want to call the mongodb connection and iterate through each entry in my list with row[i]=pk and return the other attributes as the output.
ie. mongo_documents = mongo_collection.find({'pk' : row[0]})
mongo_documents = mongo_collection.find({'pk' : row[1]})
and so on.
The code that I have is:
for row in result_set:
print row[0]
mongo_documents = mongo_collection.find({'medicalObjectId' : row[0]})
print mongo_documents
for this_document in mongo_documents:
print "----------------------------------"
print this_document
however i get no output. where am I going wrong?
if i print mongo_documents, i get
<pymongo.cursor.Cursor object at 0xe43150>
You could use the $in operator of mongodb to fetch all the rows at once and iterate through them.
mongo_documents = mongo_collection.find({ 'medicalObjectId' : { '$in' : result_set } } );
for doc in mongo_documents:
print mongo_documents
I have not tested it, comment below if it doesnt work.
EDIT
mongo_documents2 = mongo_collection.find({"medicalObjectId": str(pqr)})
print "\n\n"
for this_document2 in mongo_documents2:
print this_document2