Python Forum
Need help with Return statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with Return statement
#11
Thanks deanhystad. I copied and pasted your code and ran it to see how it worked and I get an error that says:

Error:
TypeError: can only concatenate tuple (not "List") to tuple
Yoriz write Sep-16-2022, 05:31 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#12
I use this baby every week during term, works like clock-Python-work!

Change weeknr for Recno and you have your list!

Of course, you don't need clas, but if you have various similarly named tables, maybe something like it.

import pymysql 

def mysqlRemoteCW(clas, weeknr): 
    # To connect remote MySQL database 
    conn = pymysql.connect( 
        host='111.222.333.444', 
        user='me',  
        password = 'secret', 
        db='allstudentsdb', 
        ) 
      
    cur = conn.cursor()

    # Select query 
    sql = f"SELECT studentnr, score FROM allstudentsAnswers{clas}CW WHERE weeknr = %s"
    cur.execute(sql, (weeknr,))
    output = cur.fetchall()    
    # To close the connection 
    conn.close()
    return output

results = mysqlRemoteCW(clas, weeknumber)
Reply
#13
I knew I would eventually have to make a database and test the code. Should have done that right away. Sorry.

This works:
import sqlite3

def getRecord(srchName, columns):
    """Return columns from first matching record"""
    # Wrap column names in quotes and separete by ", ".  Am wrapping
    # column names in quotes because column name might have special
    # meaning in an SQL query, like "group" for example.
    columns = ", ".join([f"\"{column}\"" for column in columns])
    query = f"SELECT {columns} FROM dino WHERE name = ?"
    c.execute(query, (srchName,))
    return c.fetchone()

conn = sqlite3.connect('dinobase.db')
c = conn.cursor()

id, group = getRecord("Entelodon",  ["recNo", "group"])
print(id, group)

conn.close()
I learned a couple things getting this to work.
1. You cannot use placeholders for column names. My idea of using "SELECT ?, ? FROM dino WHERE name = ?" did not work because you cannot use a placeholder to specify the columns you want returned.

2. "group" has special meaning. When I tried to execute "SELECT recNo, group FROM dino WHERE name =?" I got an error.
Error:
sqlite3.OperationalError: near "group": syntax error
I think it saw "group" and started looking for "GROUP BY". There are a lot of words that have special meaning in a query, so I decided to wrap all column names in double quotes: "SELECT "recNo", "group" FROM dino WHERE name = ?" works fine.

And the error in my previous post was from trying to add a list to a tuple: columns + [srchName]. Since I cannot use placeholders for column names, this is no longer a problem.
Reply
#14
Thanks deanhystad for your time and patience.

Cheers!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to invoke a function with return statement in list comprehension? maiya 4 2,749 Jul-17-2021, 04:30 PM
Last Post: maiya
  syntax error on return statement l_butler 5 3,025 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  return statement will not work TheTechRobo 2 2,588 Mar-30-2020, 06:22 PM
Last Post: TheTechRobo
  HELP! Return Statement Debugging HappyMan 5 3,059 Jan-27-2020, 07:31 PM
Last Post: michael1789
  Embedding return in a print statement Tapster 3 2,233 Oct-07-2019, 03:10 PM
Last Post: Tapster
  return statement usage SB_J 3 2,389 Jul-16-2019, 07:24 PM
Last Post: snippsat
  I don't understand this return statement 357mag 4 2,704 Jul-10-2019, 07:02 PM
Last Post: perfringo
  Return Statement in Python IDLE editor NMW 10 10,453 Jul-11-2017, 09:47 PM
Last Post: NMW
  Need help understanding return statement python_lover 4 4,324 Mar-03-2017, 10:09 AM
Last Post: python_lover
  Python basics about __init__ and return statement wk14 5 5,923 Oct-25-2016, 04:31 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020