I have two tables say Doctors and patients. I need to fetch the doctors id (unique) from the doctors table and then pass it to a column of the patient's table during the INSERT operation. How can I write the query for that.
I tried the below way but it gives programming error:
usr = self.current_user
self.db.execute("INSERT INTO patients ("SELECT id FROM doctors WHERE doctors.id = '%s' " % usr )(mrd, name, age, gender, address, phone_number, blood_group,\
registration_date, did) values ('%s', '%s', '%s', '%s','%s','%s','%s','%s', '%s')" % (mrd,name,age,gender,\
address,phoneNumber, bloodGroup,dateOfReg,usr),callback=self.add_response)
I am trying to execute this in python tornado framework with psql backend
SELECT should be in VALUES section
The query will be like so
doctor_id = self.db.execute("SELECT id FROM doctors WHERE doctors.id = '%s' " % usr ).fetchone()
self.db.execute("INSERT INTO patients(mrd, name, age, gender, address, phone_number, blood_group,registration_date, did, doctor_id) values (%s, %s, %s, %s,%s,%s,%s,%s,%s, %s)" % (mrd,name,age,gender,address,phoneNumber, bloodGroup,dateOfReg,usr, doctor_id),callback=self.add_response)
Related
I'm inserting data into a table where i'm skipping, excluding, data that is already in the table with that price and sku value.
Im running in an issue where when my variables have the same value ex. price and sale_price are both 0 or companiy_id and status are both 1 i get an error Duplicate column name.
sql_insert = "INSERT INTO apps_scraper_data_history (sku, price, company_id, date_time, status, item_key, price_sale) SELECT * FROM (SELECT %s, %s, %s, %s, %s, %s, %s) AS tmp WHERE NOT EXISTS(SELECT sku FROM apps_scraper_data_history WHERE price=%s AND sku=%s)"
val_insert = (sku, price, company, date, status, key, sale_price, price_scrape, sku)
cursor.execute(sql_insert, val_insert)
You didn't define column names for alias tmp. So that, when value of two columns are same those might be conflicting. Please try below query with column aliases.
sql_insert = "INSERT INTO apps_scraper_data_history (sku, price, company_id, date_time, status, item_key, price_sale) SELECT * FROM (SELECT %s sku, %s price, %s company_id, %s date_time, %s status, %s item_key, %s price_sale) AS tmp WHERE NOT EXISTS(SELECT sku FROM apps_scraper_data_history WHERE price=%s AND sku=%s)"
val_insert = (sku, price, company, date, status, key, sale_price, price_scrape, sku)
cursor.execute(sql_insert, val_insert)
I am trying to insert data to the table that was created earlier using python script. Here is the code I am trying to execute. I want to insert data into table with date as well.
date_today = dt.date.today()
conn = psycopg2.connect(host = serverip, port = port, database = database, user = uid, password = pwd)
cursor = conn.cursor()
cursor.execute("INSERT INTO My_TABLE (Date, Class, Total_students, failed_students, Percent_passed_students) VALUES (date_today, 'Class Name', int1, int2, int3)")
print "Data Inserted successfully"
conn.commit()
conn.close()
Here is the error I see from my job. what am i missing here?
psycopg2.ProgrammingError: column "date_today" does not exist
I created the table using different job with the following query:
cursor.execute("""CREATE TABLE MY_TABL(Date date, Lob varchar(30), Total_Students int, failed_students int, Percent_passed_students int)""")
And the table is created with above five columns.
This line:
cursor.execute("INSERT INTO My_TABLE (Date, Class, Total_students, failed_students, Percent_passed_students) VALUES (date_today, 'Class Name', int1, int2, int3)")
Is the incorrect way to dynamically insert values into a database.
Here's a functional and correct example:
cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))
And applying it in your case...
cursor.execute("INSERT INTO My_TABLE VALUES (%s, %s, %s, %s, %s)", (date_today, 'Class Name', int1, int2, int3))
I am scraping a website and getting the companies details from it, Now I trying to store the data into database. But I am getting some error like
raise InternalError(errno, errorvalue)
pymysql.err.InternalError: (1054, "Unknown column 'companyaddress' in 'field list'")
Here is my code
for d in companydetail:
lis = d.find_all('li')
companyname = lis[0].get_text().strip()
companyaddress = lis[1].get_text().strip()
companycity = lis[2].get_text().strip()
try:
companypostalcode = lis[3].get_text().strip()
companypostalcode = companypostalcode.replace(",","")
except:
companypostalcode = lis[3].get_text().strip()
try:
companywebsite = lis[4].get_text().strip()
except IndexError:
companywebsite = 'null'
print (companyname)
print (companyaddress)
print (companycity)
print (companypostalcode)
print (companywebsite)
try:
with connection.cursor() as cursor:
print ('saving to db')
cursor.execute("INSERT INTO company(companyname,address,city,pincode,website) VALUES (companyname,companyaddress,companycity,companypostalcode,companywebsite)")
connection.commit()
connection.close()
I am getting my data which I want but it I am not able to store data into database.
The result which I get while print (companyname) and print (campanyaddress) is :
NINGBO BOIGLE DIGITAL TECHNOLOGY CO.,LTD.
TIANYUAN INDUSTRIAL ZONE CIXI NINGBO
ZHEJIANGNINGBO
315325
http://www.boigle.com.cn
You cannot simply use variable names inside a query string as you do:
cursor.execute("INSERT INTO company(companyname,address,city,pincode,website) VALUES (companyname,companyaddress,companycity,companypostalcode,companywebsite)")
Instead, pass your variables into the query making it parameterized:
params = (companyname, companyaddress, companycity, companypostalcode, companywebsite)
cursor.execute("""
INSERT INTO
company
(companyname, address, city, pincode, website)
VALUES
(%s, %s, %s, %s, %s)
""", params)
In
cursor.execute("INSERT INTO company(companyname,address,city,pincode,website) VALUES (companyname,companyaddress,companycity,companypostalcode,companywebsite)")
the values in the second bracket are interpreted as table fields, rather than as python variables. Try
cursor.execute("""INSERT INTO company(
companyname,address,city,pincode,website)
VALUES (%s, %s, %s, %s, %s)""",
(companyname, companyaddress, companycity,
companypostalcode, companywebsite))
instead. You may also want to consult the docs on that.
I have a simple registration form which creates a new record in "Students" table:
cur = con.cursor()
query = "INSERT INTO students (Name, Address, Major, Phone, Email, Password) values (%s, %s, %s, %s, %s, %s)"
args = [name, address, major,phone, email, password]
cur.execute(query, args)
con.commit()
The column Id is auto-incremented, so in order to get the last Id, I select Max
query = "SELECT MAX(Id) FROM students;"
cur.execute(query)
id = cur.fetchone()[0]
It works in my little homework-project, but how would it be done in a heavy-loaded project, where there is a possibility of something being created before the select statement?
Use lastrowid attribute:
...
cur.execute(query, args)
id = cur.lastrowid
I can insert hardcoded values into an SQLite table with no problem, but I'm trying to do something like this:
name = input("Name: ")
phone = input("Phone number: ")
email = input("Email: ")
cur.execute("create table contacts (name, phone, email)")
cur.execute("insert into contacts (name, phone, email) values"), (name, phone, email)
I know this is wrong, and I can't find how to make it work. Maybe someone could point me in the right direction.
You can use ? to represent a parameter in an SQL query:
cur.execute("insert into contacts (name, phone, email) values (?, ?, ?)",
(name, phone, email))
cur.executemany("insert into contacts (name, phone, email) values (?, ?, ?)",
(name, phone, email))
cur.execute("create table contacts (name, phone, email)")
cur.execute("insert into contacts (name, phone, email) values(?,?,?)",(name, phone, email))
OR
cur.execute("insert into contacts values(?,?,?)",(name, phone, email))
As you are inserting values to all available fields, its okay not to mention columns name in insert query