Hello everyone, I have a (hopefully) quick and easy to explain question. I'm currently using MySQLdb module to retrieve some information from a database. In my case, the result that's being yield back is a single line.
As far as my understanding goes, MySQLdb function called 'fetchone()' returns the result as a tuple. Problem is, the tuple has some unnecessary characters, such as an additional comma being returned. In my case: >>> idquery = 'select id from table;' >>> cur = mysql.cursor() >>> cur.execute(idquery) >>> id = cur.fetchone() >>> print id ('idinhere',) I stumbled across an example given like this: >>> (id,) = cur.fetchone() So I decided to give it a try and the result is exactly what I need: >>> (id,) = cur.fetchone() >>> print id idinhere My question is - can I reliably use this method? Is it always going to return the string between the brackets as long as I define the variable with '(,)'? I'm planning to use another query that will be using the result from this one and then update another database with this result but I must be sure that the variable will always be the string in and between the brackets otherwise I'm risking to mess up a lot of things big time. A backup plan I had was to use the following: >>> id = cur.fetchone() >>> for x in id: >>> id = x But if the method above is certain to always return only the value I need, I find it to be a far more elegant solution. Also, just to clarify things for myself - what does this method of declaring variables do exactly? I'm sorry if this isn't the right place the ask and if this has been documented clearly already, I'm not sure what to use as a search term in order to find an answer. Thanks a lot in advance! I hope I posted all the details needed and my question is easy to comprehend. Regards, Dimitar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor