Skip to content

gh-118469: Document sqlite3.Binary #122343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tried my best
  • Loading branch information
negativenagesh committed Jul 27, 2024
commit 2d25094995fbf24ca974d30362dc69a73fdf925d
68 changes: 36 additions & 32 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ Module functions
Create a Binary object to handle binary data in SQLite.

:param data:
The binary data to be encapsulated. This should be a
The binary data to be encapsulated. This should be a
:term:`bytes-like object`.

The :func:`Binary` function encapsulates binary data to ensure proper handling
Expand All @@ -379,40 +379,44 @@ Module functions
By using :func:`Binary`, developers can ensure robust handling of binary data in SQLite,
preventing data corruption and enhancing application reliability.

**Examples:**

Basic example of storing and retrieving binary data:
**Basic example of storing and retrieving binary data:**

.. code-block:: python

import sqlite3

# Create a connection and cursor
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()

# Create a table
cursor.execute('''
CREATE TABLE files (
name TEXT,
data BLOB
)
''')

# Insert binary data
binary_data = bytes("abc", "utf-8")
query = 'INSERT INTO files (name, data) VALUES (?, ?)'
values = ('example.txt', sqlite3.Binary(binary_data))
cursor.execute(query, values)

conn.commit()

# Retrieve binary data
cursor.execute('SELECT data FROM files WHERE name=?', ('example.txt',))
blob_data = cursor.fetchone()[0]

with open('retrieved_example.txt', 'wb') as file:
file.write(blob_data)
import sqlite3
import pickle

# Create a connection and cursor
with sqlite3.connect(':memory:') as conn:
cursor = conn.cursor()

# Create a table
cursor.execute('''
CREATE TABLE files (
id INTEGER PRIMARY KEY,
name TEXT,
data BLOB)
''')

# Insert binary data
binary_data = pickle.dumps({'foo': 42, 'bar': 1337})
query = 'INSERT INTO files (name, data) VALUES (?, ?)'
values = ('example.pkl', sqlite3.Binary(binary_data))
cursor.execute(query, values)

# Retrieve binary data
cursor.execute('SELECT data FROM files WHERE name=?', ('example.pkl',))
blob_data = cursor.fetchone()[0]

# Deserialize the binary data using pickle
retrieved_data = pickle.loads(blob_data)
print(retrieved_data) # This should print: {'foo': 42, 'bar': 1337}

# Save retrieved data to a file
with open('retrieved_example.txt', 'wb') as file:
file.write(blob_data)

conn.close()

.. function:: complete_statement(statement)

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy