Updating table based on user input - python-3.x

I created a table with the following details:
myStr2 = "CREATE TABLE Lecturers (lec_no VARCHAR(3), name VARCHAR(30), email VARCHAR(30), PRIMARY KEY(lec_no))"
I then created a function that would be able to update the table.
def update_lecturer(self, field_to_update, value_to_set, lec_no):
self.field_to_update = field_to_update
self.value_to_set = value_to_set
self.lec_no = lec_no
self.myCursor.execute("UPDATE Lecturers SET field_to_update = :field_to_update WHERE lec_no =:lec_no",{'field_to_update':self.field_to_update,'value_to_set':self.value_to_set,'lec_no':self.lec_no})
The table is updated based on the user input:
field_to_update = input("Field update:")
value_to_set = input("Value to set:")
lec_no = input("Lec_no:")
where the field_to_update can be name or email, the value_to_set can be either new value of name or the new value of email.The lec_no is the id of the lecturer whose details we would like to change.
However when I run the my code I get the following error:
sqlite3.OperationalError: no such column: field_to_update
I know there is no such column as field_to_update in my table but how can I set the column to be updated based on user input.

In your last line you should replace the field_to_update with your input and the updated values.
self.myCursor.execute("UPDATE Lecturers SET :field_to_update = :value_to_set ...
(you missed a colon and the actual value_to_set)

I concatenated the variable field_to_update in the update statement then added question marks to represent the other two variables and it worked. Here is my code:
def update_lecturer(self, field_to_update, value_to_set, lec_no):
self.field_to_update = field_to_update
self.value_to_set = value_to_set
self.lec_no = lec_no
self.myCursor.execute("UPDATE Lecturers SET "+self.field_to_update+" = (?) WHERE lec_no = (?)",(self.value_to_set,self.lec_no))
self.myConnection.commit()

Related

Create column based on other values

I have columns as field.1.type = xyz, field.2.type = abc,field.1.id,field.2.type. Now I want to do the value assignment as follows.
If field.1.type contains 'xy' then create a new attribute as id_xy = field.1.id
If field.2.type contains 'ab' then create a new attribute as id_ab = field.2.id
I want to do this in a loop. How can I achieve this?

exiting lot_id in stock.move.line, showing same value from compute field

I got some issue that lot_name is not showing same value from another compute field in same recordset, stock.move.line. below is my code:
class StockMoveLine(models.Model):
_name = 'stock.move.line'
_inherit = 'stock.move.line'
lotter = fields.Char(string="Supplier Lot", compute='_compute_partner')
def _compute_partner(self):
if not self.lotter:
partner_id = self.env['stock.picking'].search([('name','=',self.reference)]).partner_id.id
self.lotter = str(partner_id)
if self.lot_name == "":
self.lot_name = self.lotter
else:
self.lot_name = "blank"
the lot_name had been already existed in base module field. So I would like to show partner_id in lot_name field as well. now I only see it in my new compute fieldl. I tried using #api.onchange but it is only work when the textfield of lotter is lost focus. how can I do to show the same value on both lotter and lot_name fields if there is no value earlier?
Add store=True in your field.Because compute field is not stored in your database.
lotter = fields.Char(string="Supplier Lot", compute='_compute_partner', store=True)

Find matching value in query based on text

I am building a table in power query and I want to find the matching value from a column in a row. Does anyone know how to do this? I import my source data with:
leagueDataSource = #"League Data All",
this gives me this table:
I then have a variable called:
leagueName = "Albania - Superliga",
and want to create another variable called activeSeason. How do I match the variable leagueName with the value in active Season ?
Found the answer myself :)
leagueName = "name",
a = List.PositionOf(leagueDataSource[League], leagueName, 0),
leagueID = Number.ToText(leagueDataSource[Active Season]{a}),

Filtering records from database using certain criteria

I created a database with a students table
myStr1 = "CREATE TABLE Students (reg_no VARCHAR(15), name VARCHAR(30), email VARCHAR(30), average_mark INTEGER, PRIMARY KEY(reg_no))"
self.myCursor.execute(myStr1)
I then added a function to add students details to the table and would like to filter out certain records based on a criteria. The following was my code:
def insert_student(self,reg_no,name,email,average_mark):
self.reg_no = reg_no
self.name = name
self.email = email
self.average_mark = average_mark
self.myCursor.execute("INSERT INTO Students VALUES (:reg_no, :name, :email, :average_mark)",{'reg_no':self.reg_no,'name':self.name,'email':self.email,'average_mark':self.average_mark})
self.myConnection.commit()
def get_students(self,criteria = None):
self.criteria = criteria
self.myCursor.execute("SELECT * FROM Students WHERE '{}'".format(self.criteria))
return self.myCursor.fetchall()
myWrapper = DBWrapper()
myWrapper.insert_student('F17/42770/2017','Mutheu Lorraine','mutheu#gmail.com',100)
myWrapper.insert_student('F17/3030/2015','Ron Weasley', 'ron#eie.com',71)
myWrapper.insert_student('F17/3031/2015','Hermione Granger', 'hermine#eie.com',71)
myWrapper.insert_student('F17/3031/2017','Ginny Weasley', 'ginny#eie.com',71)
myWrapper.myConnection.commit()
students = myWrapper.get_students('reg_no = F17/42770/2017')
print(students)
However when i run my code I only get '[]' instead of all the details of the student with the given registration number. How should I go about this?
After the formatting, the resulting SQL command is:
SELECT * FROM Students WHERE 'reg_no = F17/42770/2017'
The entire WHERE clause is a string, and that string does not evaluate to true for any row.
To make the criteria an SQL expression, drop the quotes. But the comparison value must be quoted so that it is recognized as a string:
self.myCursor.execute("SELECT * FROM Students WHERE {}".format(self.criteria))
...
students = myWrapper.get_students("reg_no = 'F17/42770/2017'")

Removing a column from result set in Groovy Sql rows

I have a query where I need to retrieve a column which I need only temporarily because I need to pass it in the parameter for a where clause, how can I remove this column and its value from my result set after it served that purpose. Hopefully the code will show what I mean...
def empQuery = "select id, name, address from Employee"
def retObj = [:]
def sql = new Sql(datasource)
retObj = sql.rows(empQuery.toString())
retObj.each {
def addressQuery = "select street from Address where employe_id = ${it['id']}
// at this point I want to remove 'id:n' from the result set hash map aka 'it'
}
Because later on I am displaying that result set on a page for the user, and the ID field is not relevant.
So can you please show the Groovy code to remove a column and its value from the rows data structure returned from sql.rows?
from http://docs.groovy-lang.org/latest/html/api/groovy/sql/GroovyRowResult.html
It looks like you can do something line:
retObj.each { it.remove('id')}
However I haven't tried it....

Resources