![]() Password = hasattr(options, 'db_password') and "password=%s" % options.db_password or '' db = psycopg. "ALTER TABLE res_currency DROP rounding_bak", "UPDATE res_currency SET rounding = power(10, - rounding_bak)", "ALTER TABLE res_currency ADD rounding NUMERIC(12,6)", "ALTER TABLE res_currency RENAME rounding TO rounding_bak", import pymssql conn nnect (serverserver, useruser, passwordpassword, databasedb) cursor conn. # - # change currency rounding # - #Ĭr.execute( """SELECT c.relname,a.attname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,t.typname,CASE WHEN a.attlen=-1 THEN a.atttypmod-4 ELSE a.attlen END as size FROM pg_class c,pg_attribute a,pg_type t WHERE c.relname='res_currency' AND a.attname='rounding' AND c.oid=a.attrelid AND a.atttypid=t.oid""") Well depending on what sql database you are using you can pip install pymssql for microsoft sql (mssql), psycopg2 for postgres (psql) or mysqldb for mysql databases Here are a few examples of using it. # - # change case date_deadline rounding # - # # - # drop not null on ir_attachment # - # # - # remove ir_value # - #Ĭr.execute( "delete from ir_values where model = 'ir.ui.menu' and res_id is null") ![]() # - # remove old menu # - #Ĭr.execute( "delete 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'))") Ids = ','.join( map( lambda x: str(x), cr.fetchall()))Ĭr.execute( 'UPDATE res_partner_address set country_id = %d where country_id in ('+ids+ ')', (res2,))Ĭr.execute( 'DELETE FROM res_country WHERE code = %s and id <> %d', (c, res2,))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'): Print('Connection not established to PostgreSQL.If not ( hasattr(options, name) and getattr(options, name)): Print('Connection established to PostgreSQL.') connect function returns a connection object which can be used to run SQL queries on the database.Ĭonn = nnect(host="localhost",database="mydb", user="postgres", password="postgres").host specifies the machine on which the PostgreSQL server is running database specifies the specific database among many databases to which connection has to be made user and password are the credentials to access the database. Call connect method on psycopg2 with the details: host, database, user and password.To connect to a PostgreSQL database from Python application, follow these steps. Since you are using keyword arguments (the second option in the documentation), you should specify the host without the port and the database and see if that works. ![]() Psycopg2 library is one of the most widely used library for connecting and performing operations on PostgreSQL database. You can connect using the dsn parameter or you can use keyword arguments. In this tutorial, we will learn how to connect to PostgreSQL Database from Python application using psycopg2 library. Connect to PostgreSQL from Python with psycopg2
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |