![]() ![]() Raise Exception( 'This script is provided as an example, you must custom it before')Ĭr.execute( 'SELECT code from res_country where code is not null group by code')Ĭr.execute( 'SELECT max(id) from res_country where code = %s group by code', (c,))Ĭr.execute( 'SELECT id from res_country where code = %s', (c,)) # - # remove old menu # - # while True:Ĭr.execute( "select id from ir_ui_menu where (id NOT IN (select parent_id from ir_ui_menu where parent_id is not null)) and (id NOT IN (select res_id from ir_values where model='ir.ui.menu'))") # - # move user id from hr_analytic_timesheet to account_analytic_line # - #Ĭr.execute( "UPDATE account_analytic_line SET user_id = hr_analytic_er_id FROM hr_analytic_timesheet WHERE hr_analytic_timesheet.line_id = account_analytic_line.id") Password = hasattr(options, 'db_password') and "password=%s" % options.db_password or '' db = nnect( '%s %s %s %s %s' % (host, port, name, user, password), serialize= 0) User = hasattr(options, 'db_user') and "user=%s" % options.db_user or '' Port = hasattr(options, 'db_port') and "port=%s" % options.db_port or '' Host = hasattr(options, 'db_host') and "host=%s" % options.db_host or '' Value = False setattr(options, name, value) Value = True if value in ( 'false', 'False'): Sql.SQL(',').join(map(sql.If not ( hasattr(options, name) and getattr(options, name)): It was introduced in the version 2.7 from psycopg2 import sqlĬolumns = ('country_name_ru', 'airport_name_ru', 'city_code') 'SELECT * FROM engine_airport WHERE city_code = %(city_code)s',Īlso psycopg2 provides the module called sql which can be used to securely form an SQL query. If you need to use the character % you have to write it as %%.Do not use single quotes for string values.Placeholder should be %s for all data types.You should keep in mind the following rules while working with placeholders: ![]() With conn.cursor(cursor_factory=DictCursor) as cursor:ĭid you know that you can form a query using psycopg2? You can do it using format-like placeholders: cursor.execute('SELECT * FROM airport WHERE city_code = %s', ('ALA', )) If you want to get a value by column name you can use NamedTupleCursor or DictCursor: from psycopg2.extras import DictCursor 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 |