본문 바로가기

Development (Python, Django, C..)

[python] HANA DB 접속하기

참고 : https://help.sap.com/viewer/1efad1691c1f496b8b580064a6536c2d/Cloud/en-US/d12c86af7cb442d1b9f8520e2aba7758.html

 

SAP Help Portal

 

help.sap.com

 


 

HANA DB 접속 방법

1. Python Driver 설치

 

2. Import dbapi module

from hdbcli import dbapi

3. Use connect method

dbapi.connect(address='localhost', port=30015, user='system', password='manager')

 

 

 

Connection Properties Specific to the Python Driver

Property Value Default Description

address

<string>

Empty string

Specifies the IP address or host name of the database instance.
This property is case sensitive.

autocommit

boolean

True

Sets the autocommit mode for the connection. In autocommit mode, every statement is automatically committed. Otherwise, commits and/or rollbacks must be done manually.

This property is case sensitive.

packetSize

integer

None

Sets the communication packet size for the connection.

password

<string>

Empty string

Specifies the password for the user.
This property is case sensitive.

port

boolean

0

Specifies the port number of the database instance.
This property is case sensitive.

properties

<string>

 

additional dict object with special properties

user

<string>

Empty string

Specifies the user name.
This property is case sensitive.

userkey

<string>

None

Specifies the user key for the database. The user key is case insensitive.
This property is case sensitive.

 

 

SQLDBC Connection Properties

Property Value Default Description
abapVarCharMode

Boolean

FALSE

Specifies that in ABAP VARCHAR mode, a single space and an empty string are considered the same.

chopBlanks

Boolean

FALSE

Removes trailing blanks from character output.

chopBlanksInput

Boolean

FALSE

Removes trailing blanks from character input.

compress

Boolean

FALSE

Enables or disables network compression for the connection. The indexserver.ini > session > compression server configuration determines the default compression setting for the connection. Setting this parameter overrides the setting of the indexserver.ini > session > compression parameter. Clients running on the same machine as the server never use network compression even if compression is requested.

connectTimeout

Timeout in milliseconds

0 (use system's TCP/IP socket connection timeout)

Aborts connection attempts after the specified timeout.

currentSchema

<schema>

CURRENTUSER

Sets the current schema, which is used for identifiers without a schema.

databaseName

<name>

 

Specifies the name of the database to connect to.

distribution

OFF, CONNECTION, STATEMENT, ALL

STATEMENT

Specifies the distribution mode. Specifying STATEMENT does not include CONNECTION distribution.

emptyTimestampIsNull

Boolean

TRUE

When enabled, DATE, TIME, SECONDDATE, and TIMESTAMP values inserted as empty strings are returned as NULLs. When disabled, these values are returned as the following out-of-band values:

  • DATE - one day before 0001-01-01 00:00:00.0000000 UTC AD, which is 0001-12-31 00:00.000000000 UTC BC

  • TIME - one second before 0001-01-01 00:00:00.0000000 UTC AD, which is 0001-12-31 23:59:59.000000000 UTC BC

  • SECONDDATE - one second before 0001-01-01 00:00:00.0000000 UTC AD, which is 0001-12-31 23:59:59.000000000 UTC BC

  • TIMESTAMP - one hundred nanoseconds before 0001-01-01 00:00:00.0000000 UTC AD, which is 0001-12-31 23:59:59.999999900 UTC BC

ignoreTopology

Boolean

FALSE

Controls how the client uses the topology returned from the server. TRUE indicates to use the topology unless port-forwarding is detected and FALSE indicates to always ignore the topology.

It is an error to specify both ignoreTopology=TRUE and siteType=PRIMARY or SECONDARY.

isolationLevel

TRANSACTION_READ_COMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE

TRANSACTION_READ_COMMITTED

Sets the isolation level for the connection.

key

<string>

 

Specifies the user key to use for the database. The user key is case insensitive.

locale

ISO locale code

client locale

Specifies the client locale.

nodeConnectTimeout

Timeout in milliseconds

0 (use system's TCP/IP socket connect timeout)

Aborts connection attempts to each specified server address (if multiple server addresses are specified, or if additional addresses are known from the server topology) after the specified timeout. If connection attempts to a particular server address are slow to fail, then using this connection property reduces the time before a successful connection by minimizing the time waiting to connect to individual addresses.

packetSize

<number> in bytes

1048576 (1 MB)

Sets the maximum size of a request packet sent from the client to the server, in bytes. In some cases, request packets can be larger (for example, if a SQL statement or parameter value is larger than the packetSize). The minimum is 1 MB.

prefetch

Boolean

TRUE

Controls result set prefetching. When enabled, result set prefetch requests the next block of rows of a result set while the application is still processing the current block of rows. When fetching large result sets, result set prefetch can improve performance; however, it can also result in the client using more memory. In some cases, the server SqlExecutor thread could be blocked from sending the reply for an indefinite period of time until the client receives the prefetch reply. If the result set contains LOB columns, then result set prefetch is always disabled.

proxy_host

<host-name>

 

Specifies the host name of the proxy server.

proxy_password

<password>

 

Specifies the password for METHOD 02 authentication.

proxy_port

<port-number>

 

Specifies the port of the proxy server. The default is 1080.

proxy_scp_account

<subaccount>.<location-ID>

 

Provides SAP Cloud Connector routing information to the proxy for the SAP Cloud Platform. <subaccount> is mandatory and <locationID> is optional. Both values are entered as strings and are converted to base64 encoding.

proxy_userid

<user-name>

 

Specifies the user name for METHOD 02 authentication.

reconnect

Boolean

TRUE

Controls whether the system automatically reconnects to the database instance after a command timeout or when the connection is broken. Reconnecting restores the old state (for example, if no transaction was open).

sessionVariable:<key-name>

<session variable value>

None except for APPLICATION and APPLICATIONUSER keys

Sets the session variable <key-name> to <session variable value>. For example, to set the session variable with key APPLICATION, use sessionVariable:APPLICATION=MyApplication.

siteType

PRIMARY, SECONDARY

None

Specifies whether the connection is made to either the PRIMARY or SECONDARY site in an Active/Active (read enabled) system, skipping any of the <host>:<port> locations that are for a different site type.

You must specify this property for takeover/failover situations that use connection distribution or non-virtual IP addresses.

It is an error to set both ignoreTopology and siteType.

splitBatchCommands

Boolean

TRUE

Allows split and parallel execution of batch commands on partitioned tables.

statementCacheSize

<number>

0 (no statements cached)

Controls the maximum number of cached prepared statements. Caching SQL statements that are frequently prepared and dropped can improve performance. DDL can cause behavior changes when statement caching is enabled.

NoteIf it is possible that the definition of objects referred to by the prepared statements may change because DDL is executed while you are connected, then it is recommended that you do not enable prepared statement caching.
webSocketURL

<webSocketUrl>

 

Specifies the WebSocket URL used to connect to a single-node SAP HANA server. The connection property value cannot be empty.

WebSockets connections have the following default connection property settings:

  • encrypt=TRUE

  • compress=TRUE

  • ignoreTopology=TRUE

  • reconnect=FALSE