-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further work on adjusting attribute, method and parameter names to be
consistent and to comply with PEP 8 naming guidelines; also adjust implementation of #385 (originally done in pull request #549) to use the parameter name `bypass_decode` instead of `bypassencoding`.
- Loading branch information
1 parent
ab6e6f0
commit 96f9382
Showing
7 changed files
with
155 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#------------------------------------------------------------------------------ | ||
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
#------------------------------------------------------------------------------ | ||
|
||
#------------------------------------------------------------------------------ | ||
# query_strings_as_bytes.py | ||
# | ||
# Demonstrates how to query strings as bytes (bypassing decoding of the bytes | ||
# into a Python string). This can be useful when attempting to fetch data that | ||
# was stored in the database in the wrong encoding. | ||
# | ||
# This script requires cx_Oracle 8.2 and higher. | ||
#------------------------------------------------------------------------------ | ||
|
||
import cx_Oracle as oracledb | ||
import sample_env | ||
|
||
STRING_VAL = 'I bought a cafetière on the Champs-Élysées' | ||
|
||
def return_strings_as_bytes(cursor, name, default_type, size, precision, | ||
scale): | ||
if default_type == oracledb.DB_TYPE_VARCHAR: | ||
return cursor.var(str, arraysize=cursor.arraysize, bypass_decode=True) | ||
|
||
with oracledb.connect(sample_env.get_main_connect_string()) as conn: | ||
|
||
# truncate table and populate with our data of choice | ||
with conn.cursor() as cursor: | ||
cursor.execute("truncate table TestTempTable") | ||
cursor.execute("insert into TestTempTable values (1, :val)", | ||
val=STRING_VAL) | ||
conn.commit() | ||
|
||
# fetch the data normally and show that it is returned as a string | ||
with conn.cursor() as cursor: | ||
cursor.execute("select IntCol, StringCol from TestTempTable") | ||
print("Data fetched using normal technique:") | ||
for row in cursor: | ||
print(row) | ||
print() | ||
|
||
# fetch the data, bypassing the decode and show that it is returned as | ||
# bytes | ||
with conn.cursor() as cursor: | ||
cursor.outputtypehandler = return_strings_as_bytes | ||
cursor.execute("select IntCol, StringCol from TestTempTable") | ||
print("Data fetched using bypass decode technique:") | ||
for row in cursor: | ||
print(row) |
Oops, something went wrong.