简体   繁体   中英

SELECT * FROM table_name give me only one column as result

I'm using python 2.7 and sqlite3 as DB, I am trying to call from this table :

在此处输入图片说明

using this code :

conn = sqlite3.connect('storage/container.db')
conn.row_factory = lambda c, row: row[0]
c = conn.cursor()
c.execute("SELECT * FROM reports")
for row in c.fetchall():
    print row

and the result is :

0
1204

which is only the id column, despite I am using fetchall() and used SELECT *

update

After removing conn.row_factory = lambda c, row: row[0] the result is :

(0, u'\u062f\u062e\u0648\u0644', u'\u0643\u0631\u0633\u0649', 8, u'\u0623\u062d\u0645\u062f \u0648\u062c\u062f\u0649', u'2018-03-28')
(1204, u'\u062e\u0631\u0648\u062c', u'\u0643\u0631\u0633\u0649', 3, u'\u0623\u062d\u0645\u062f \u0648\u062c\u062f\u0649', u'2018-03-28')

then tried to add utf-encoding to the connection but still the same result, i used c.execute('PRAGMA encoding="UTF-8";')

Found the answer, as a tuple item cannot accept-encoding, I applied utf-8 encoding # -*- coding: utf-8 -*- to the whole document and extracted names one by one as str from the list and the result is now fixed :

here is the updated code :

# -*- coding: utf-8 -*-

import sqlite3 

conn = sqlite3.connect('storage/container.db')
c = conn.cursor()
c.execute("SELECT * FROM reports")
for row in c.fetchall():
    for names in row:
        print names
  • In the mentioned issue.

    conn.row_factory = lambda c, row: row[0]

You are explicitly calling first row[0] you are getting only one column. Either try by row[0],row[1],row[2] depending on number of colums.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM