मैं चयनित कॉलम नामों से सीधे कॉलम लेबल जेनरेट करने का एक सामान्य तरीका चाहता हूं, और यह देखते हुए कि पाइथन का psycopg2 मॉड्यूल इस सुविधा का समर्थन करता है।मैं psycopg2 कर्सर से कॉलम नामों की सूची कैसे प्राप्त करूं?
उत्तर
"प्रोग्रामिंग अजगर" मार्क लुट्ज़ द्वारा से:
curs.execute("Select * FROM people")
colnames = [desc[0] for desc in curs.description]
करने के लिए एक अलग क्वेरी में स्तंभ नाम, आप information_schema.columns तालिका क्वेरी कर सकते हैं।
#!/usr/bin/env python3
import psycopg2
if __name__ == '__main__':
DSN = 'host=YOUR_DATABASE_HOST port=YOUR_DATABASE_PORT dbname=YOUR_DATABASE_NAME user=YOUR_DATABASE_USER'
column_names = []
data_rows = []
with psycopg2.connect(DSN) as connection:
with connection.cursor() as cursor:
cursor.execute("select field1, field2, fieldn from table1")
column_names = [desc[0] for desc in cursor.description]
for row in cursor:
data_rows.append(row)
print("Column names: {}\n".format(column_names))
मैं देखा है कि आप करने के लिए क्वेरी के बाद cursor.fetchone()
का उपयोग करना चाहिए: के रूप में डेटा पंक्तियों समान क्वेरी में
#!/usr/bin/env python3
import psycopg2
if __name__ == '__main__':
DSN = 'host=YOUR_DATABASE_HOST port=YOUR_DATABASE_PORT dbname=YOUR_DATABASE_NAME user=YOUR_DATABASE_USER'
column_names = []
with psycopg2.connect(DSN) as connection:
with connection.cursor() as cursor:
cursor.execute("select column_name from information_schema.columns where table_schema = 'YOUR_SCHEMA_NAME' and table_name='YOUR_TABLE_NAME'")
column_names = [row[0] for row in cursor]
print("Column names: {}\n".format(column_names))
को पाने के स्तंभ नाम, आप कर्सर के वर्णन क्षेत्र का उपयोग कर सकते cursor.description
में स्तंभों की सूची प्राप्त ([desc[0] for desc in curs.description]
में यानी)
#!/usr/bin/python
import psycopg2
#note that we have to import the Psycopg2 extras library!
import psycopg2.extras
import sys
def main():
conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"
# print the connection string we will use to connect
print "Connecting to database\n ->%s" % (conn_string)
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object, you can use this query to perform queries
# note that in this example we pass a cursor_factory argument that will
# dictionary cursor so COLUMNS will be returned as a dictionary so we
# can access columns by their name instead of index.
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
# tell postgres to use more work memory
work_mem = 2048
# by passing a tuple as the 2nd argument to the execution function our
# %s string variable will get replaced with the order of variables in
# the list. In this case there is only 1 variable.
# Note that in python you specify a tuple with one item in it by placing
# a comma after the first variable and surrounding it in parentheses.
cursor.execute('SET work_mem TO %s', (work_mem,))
# Then we get the work memory we just set -> we know we only want the
# first ROW so we call fetchone.
# then we use bracket access to get the FIRST value.
# Note that even though we've returned the columns by name we can still
# access columns by numeric index as well - which is really nice.
cursor.execute('SHOW work_mem')
# Call fetchone - which will fetch the first row returned from the
# database.
memory = cursor.fetchone()
# access the column by numeric index:
# even though we enabled columns by name I'm showing you this to
# show that you can still access columns by index and iterate over them.
print "Value: ", memory[0]
# print the entire row
print "Row: ", memory
if __name__ == "__main__":
main()
मैं अल इसलिए इसी तरह के मुद्दे का सामना करने के लिए इस्तेमाल किया जाता है। मैं इसे हल करने के लिए एक सरल चाल का उपयोग करता हूं। मान लीजिए आप की तरह
col_name = ['a', 'b', 'c']
फिर एक सूची में कॉलम नाम आप निम्न कर सकते 2,7
total_fields = len(cursor.description)
fields_names = [i[0] for i in cursor.description
Print fields_names
डीबी क्वेरी से नामांकित ओपीजे रखने के लिए आप निम्न स्निपेट का उपयोग कर सकते हैं:
from collections import namedtuple
def create_record(obj, fields):
''' given obj from db returns namedtuple with fields mapped to values '''
Record = namedtuple("Record", fields)
mappings = dict(zip(fields, obj))
return Record(**mappings)
cur.execute("Select * FROM people")
colnames = [desc[0] for desc in cur.description]
rows = cur.fetchall()
cur.close()
result = []
for row in rows:
result.append(create_record(row, colnames))
इस रिकॉर्ड मूल्यों करती हैं वे वर्ग गुण यानी रहे थे के रूप में अगर
record.id, record.other_table_column_name, आदि
या भी कम आप allowes
from psycopg2.extras import NamedTupleCursor
with cursor(cursor_factory=NamedTupleCursor) as cur:
cur.execute("Select * ...")
return cur.fetchall()
में लिखा यदि आप चाहते हैं अजगर स्क्रिप्ट निम्न SQL क्वेरी लिखने को क्रियान्वित करने के बाद
for row in cursor.fetchone():
print zip(col_name, row)
एक और चीज जो आप कर सकते हैं वह एक कर्सर बनाना है जिसके साथ आप अपने कॉलम को उनके नामों से संदर्भित कर सकेंगे (यह एक ज़रूरत है जिसने मुझे इस पृष्ठ पर ले जाया पहली जगह):
import psycopg2
from psycopg2.extras import RealDictCursor
ps_conn = psycopg2.connect(...)
ps_cursor = psql_conn.cursor(cursor_factory=RealDictCursor)
ps_cursor.execute('select 1 as col_a, 2 as col_b')
my_record = ps_cursor.fetchone()
print (my_record['col_a'],my_record['col_b'])
>> 1, 2
- 1. sqlite3/iPhone पर कॉलम नामों की सूची कैसे प्राप्त करें?
- 2. LINQ का उपयोग कर डेटाटेबल से नामों की एक अलग, आदेशित सूची कैसे प्राप्त करूं?
- 3. डब्ल्यूसीएफ: मैं सर्विसहोस्ट से एंडपॉइंट्स की सूची कैसे प्राप्त करूं?
- 4. मैं VB.NET में चल रहे प्रक्रिया नामों की सूची कैसे प्राप्त करूं?
- 5. मैं दो विधानसभा नामों की तुलना कैसे करूं?
- 6. उचित नामों की सूची?
- 7. वसंत से सभी तालिका नामों की सूची प्राप्त करें SimpleJdbcTemplate
- 8. मैं स्थापित अद्यतनों और हॉटफिक्सेस की सूची कैसे प्राप्त करूं?
- 9. मैं सभी उपलब्ध खोल कमांडों की सूची कैसे प्राप्त करूं
- 10. कर्सर लूप में उपयोग के लिए मैं एकाधिक कॉलम कैसे प्राप्त करूं?
- 11. jQuery: मैं अपने कर्सर के नीचे तत्व कैसे प्राप्त करूं?
- 12. psycopg2 का उपयोग करके पोस्टग्रेज़ में टेबल कैसे प्राप्त करूं?
- 13. मैं वर्तमान स्क्रीन कर्सर स्थिति कैसे प्राप्त करूं?
- 14. मैं दो कॉलम jquery स्वतः पूर्ण कैसे प्राप्त करूं?
- 15. मैं 'git filter-branch' से पुराने-> नए पुनर्लेखित एसएचए की सूची कैसे प्राप्त करूं?
- 16. psycopg2
- 17. मैं Google ऐप इंजन पर नेमस्पेस की सूची कैसे प्राप्त करूं?
- 18. कैसे निष्पादित एसक्यूएल को पार्स पहले बिना psycopg2.extras.RealDictCursor
- 19. कर्सर
- 20. मैं प्रयुक्त सीमा में एक संपूर्ण कॉलम कैसे प्राप्त करूं?
- 21. मैं ओडीबीसी कनेक्शन से उपलब्ध तालिकाओं की सूची कैसे प्राप्त करूं?
- 22. प्रिंटर कतार से मैं सूची या नौकरियों की संख्या कैसे प्राप्त करूं?
- 23. मैं एसवीएन से सभी अपरिवर्तित फ़ाइलों की सूची कैसे प्राप्त करूं?
- 24. मैं शब्दकोश से कैसे प्राप्त करूं?
- 25. मैं वीबीए से SQLite कैसे प्राप्त करूं?
- 26. मैं XDocument से नेमटेबल कैसे प्राप्त करूं?
- 27. मैं UIImagePickerControllerReferenceURL से डेटा कैसे प्राप्त करूं?
- 28. मैं कैस्परजेएस से आईफ्रेम कैसे प्राप्त करूं?
- 29. कोर सेवा वाले उपयोगकर्ताओं की सूची कैसे प्राप्त करूं?
- 30. मैं एनएसएसटींग से कक्षा कैसे प्राप्त करूं?
यदि आप केवल कॉलम नाम चाहते हैं, तो तालिका में सभी पंक्तियों का चयन न करें। यह अधिक कुशल है: 'curs.execute ("चुनें * लोगों से LIMIT 0") ' – Demitri
यह जोड़ना उचित हो सकता है कि यह दृश्यों के साथ-साथ तालिकाओं के लिए भी काम करता है, जबकि दृश्यों के लिए कॉलम नाम प्राप्त करना संभव नहीं है 'information_schema'। – wjv
एक विशेषता के रूप में नाम प्राप्त करने के लिए और अधिक सहज हो सकता है: colnames = [curs.description में desc के लिए desc.name] – dexgecko