Posts

Showing posts with the label python mysqldb

MySQLdb Warning: Field x doesn't have a default value

Today while I was working on a Python program that uses mysql database, I got a new type of warning while inserting data into a table: /usr/lib/python2.7/dist-packages/MySQLdb/cursors.py:206: Warning: Field 'id' doesn't have a default value   r = r + self.execute(query, a) Then I looked into the mysql table and found that the field 'id' was supposed to have a default value (primary key, auto increment). But somehow it didn't have. That was the reason of the warning and it got vanished after I fixed the table. Hope my experience will save your time if you get into this problem. Happy coding! :)

mysqldb cursor.fetchall returns tuple not list

In Python MySQLdb module, cursor.fetchall() returns a tuple. For example: >>> query = "SELECT name FROM table ..." >>> cursor.execute(query) >>> result = cursor.fetchall() >>> result (('autos',), ('books',), ('health care products',), ('sports equipment',)) >>> But often we require the result to be a list instead of tuple. I don't know any straight forward way to do this. So I used the following code: >>> li = [x[0] for x in result] >>> li ['autos', 'books', 'health care products', 'sports equipment'] >>> Please let me know if there is any better way!