![]() With closing(nnect(.)) as conn:Ĭursor.execute('SELECT * FROM airport LIMIT 5')īy default when you iterate over a cursor (or using the methods mentioned above) you will get a tuple, each column corresponds to its index. Let's do it in a pythonic way using the context manager: from contextlib import closing If you want to follow best practices you need to close a cursor and a connection. cursor.fetchmany(size=5) - returns the provided number of rowsĬursor is an iterable object, so you can use the for loop: for row in cursor:.cursor.fetchall() - returns a list of rows.cursor.fetchone() - returns a single row.When a query is sent you can get the results using the following methods: Now you have to use cursor to make queries: cursor.execute('SELECT * FROM airport LIMIT 10') In order to query a database first we need to connect to it and get a cursor: import psycopg2Ĭonn = nnect(dbname='database', user='db_user', If you install the psycopg2 you have to have additional source files and compiler (gcc):īut you can install the precompiled binary, in this case you need to execute: pip install psycopg2-binary It is written in C programming language using libpq. In order "to speak" with a PostgreSQL database pythonistas usually use psycopg2 library. Python community likes PostgreSQL as well as PHP community likes MySQL. ![]() If you are building a web application you need a database. PostgreSQL is one of the most popular open source database.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |