{"id":8573,"date":"2023-11-07T18:32:15","date_gmt":"2023-11-07T13:02:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=8573"},"modified":"2023-11-10T12:21:52","modified_gmt":"2023-11-10T06:51:52","slug":"python-data-presistence-row-object","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-presistence-row-object\/","title":{"rendered":"Python Data Presistence – Row Object"},"content":{"rendered":"
By default, each row in the query result set is a tuple of values belonging to the column list in the SELECT statement. In the above example, the row object returns a tuple.<\/p>\n
Example<\/strong><\/p>\n The order of columns in the tuple cannot be ascertained from the object itself. The connection object has a useful \u2018row_\u00a3actory\u2019 property with which row in the result set can be converted into some meaningful representation. This can be done either by assigning a row factory to a user-defined function that will return a custom object or by setting it to the constructor of the Row class.<\/p>\n Row class has been defined in the sqlite3 module, whose primary purpose is to be used as a row factory. As a result, the row of the result set is returned as a Row object. Row class defines a keys () method that returns column names used in the SELECT statement. Values are accessible using the index as well as by name.<\/p>\n Example<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python Data Presistence – Row Object By default, each row in the query result set is a tuple of values belonging to the column list in the SELECT statement. In the above example, the row object returns a tuple. Example >>> row=cur. f etchone ( ) > > > row (2, ‘TV’, 40000) >>> type(row) …<\/p>\n>>> row=cur. f etchone ( )\r\n> > > row\r\n(2, 'TV', 40000)\r\n>>> type(row)\r\n<class 'tuple'><\/pre>\n
>>> r=cur.fetchone( )\r\n>>> type(r)\r\n<class 'sqlite3.Row'>\r\n>>> r.keysO\r\nt'ProductID', 'Name', 'Price']\r\n>>> fields=r .keys ( )\r\n>>> r[1]\r\n'TV'\r\n> > > r['name']\r\n'TV'\r\n>>> for nm in fields:\r\nprint (nm, r[nm])\r\nProductID 2\r\nName TV\r\nPrice 40000<\/pre>\n