Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/docs/_sources/getting-started.rst.txt
469 views
1
.. currentmodule:: singlestoredb
2
.. ipython:: python
3
:suppress:
4
import singlestoredb as s2
5
conn = s2.connect()
6
Getting Started
7
===============
8
Database connections can be made using either keyword parameters or
9
a URL as described in the following sections.
10
Connect using DB-API Parameters
11
-------------------------------
12
Connections to SingleStoreDB can be made using the parameters described in
13
the `Python DB-API <https://peps.python.org/pep-0249/>`_. The ``host=``
14
parameter can be either a hostname or IP address (it can also be a
15
URL as shown in the following section). The ``port=`` parameter is an
16
integer value of the database server port number. The ``user=`` and
17
``password=`` parameters specify the database user credentials. The
18
``database=`` parameter, optionally, specifies the name of the database
19
to connect to.
20
A full list of connection parameters can be seen in the API documentation
21
for the :func:`singlestoredb.connect` function.
22
.. ipython:: python
23
:verbatim:
24
import singlestoredb as s2
25
conn = s2.connect(host='...', port='...', user='...',
26
password='...', database='...')
27
Connect using a URL
28
-------------------
29
In addition, you can user a URL like in the SQLAlchemy package.
30
.. ipython:: python
31
:verbatim:
32
conn = s2.connect('user:password@host:port/database')
33
URLs work equally well to connect to the
34
`Data API <https://docs.singlestore.com/managed-service/en/reference/data-api.html>`_.
35
.. ipython:: python
36
:verbatim:
37
conn = s2.connect('https://user:password@host:port/database')
38
Specifying Additional Connection Parameters
39
-------------------------------------------
40
Connection parameters can be set either in the URL or
41
as parameters. Here ``local_infile=`` is set as a URL parameter.
42
.. ipython:: python
43
:verbatim:
44
conn = s2.connect('https://user:password@host:port/database?local_infile=True')
45
In this example, ``local_infile=`` and user credentials are specified
46
as keyword parameters.
47
.. ipython:: python
48
:verbatim:
49
conn = s2.connect('https://host:port/database', user='...', password='...',
50
local_infile=True)
51
Executing Queries
52
-----------------
53
Once you have a connection established, you can query the database.
54
As defined in the DB-API, a cursor is used to execute queries and fetch
55
the results.
56
.. ipython:: python
57
with conn.cursor() as cur:
58
cur.execute('show variables like "auto%"')
59
for row in cur.fetchall():
60
print(row)
61
Parameter Substitution
62
......................
63
If your queries require parameter substitutions, they can be specified in
64
one of two formats: named (``%(name)s``) or positional (``%s``).
65
.. warning:: As of v0.5.0, the substition parameter has been changed from
66
``:1``, ``:2``, etc. for list parameters and ``:foo``, ``:bar``, etc.
67
for dictionary parameters to ``%s`` and ``%(foo)s``, ``%(bar)s``, etc.
68
respectively, to ease the transition from other MySQL Python packages.
69
Named Substitution
70
^^^^^^^^^^^^^^^^^^
71
When named parameters
72
are used, the data structure passed to the :meth:`Cursor.execute` method
73
must be a dictionary, where the keys map to the names given in the substitutions
74
and the values are the values to substitute.
75
In the example below, ``%(pattern)s`` is replaced with the value ``"auto%"``. All
76
escaping and quoting of the substituted data values is done automatically.
77
.. ipython:: python
78
with conn.cursor() as cur:
79
cur.execute('show variables like %(pattern)s', dict(pattern='auto%'))
80
for row in cur.fetchall():
81
print(row)
82
Positional Substitution
83
^^^^^^^^^^^^^^^^^^^^^^^
84
If positional parameters are used, the data structure passed to the
85
:meth:`Cursor.execute` method must be a list or tuple with the same
86
number of elements as there are ``%s`` values in the query string.
87
In the example below, ``%s`` is replaced with the value ``"auto%"``. All
88
escaping and quoting of the substituted data values is done automatically.
89
.. ipython:: python
90
with conn.cursor() as cur:
91
cur.execute('show variables like %s', ['auto%'])
92
for row in cur.fetchall():
93
print(row)
94
Fetching Results
95
----------------
96
Fetching results can be done in a number of ways. The DB-API specifies three methods
97
that can be used to fetch results: :meth:`Cursor.fetchone`, :meth:`Cursor.fetchall`,
98
and :meth:`Cursor.fetchmany`.
99
The :meth:`Cursor.fetchone` method fetches a single row of data returned by a query.
100
The :meth:`Cursor.fetchall` method fetches all of the results of a query. The
101
:meth:`Cursor.fetchmany` method fetches a specified number of rows to retrieve.
102
The choice of which one to use depends mostly on the expected size of the result.
103
If the result is expected to be fairly small, fetching the entire result in one
104
call may be fine. However, if the query result will be large enough to put a strain
105
on the client computer's memory, it may be a better idea to fetch smaller batches
106
using :meth:`Cursor.fetchmany`.
107
In additon to the DB-API methods for fetching results, a :class:`Cursor` can be
108
iterated over itself.
109
.. ipython:: python
110
with conn.cursor() as cur:
111
cur.execute('show variables like "auto%"')
112
for row in cur:
113
print(row)
114
Result Type
115
...........
116
In addition to being able to specify the amount of data to be retrieved, you can also
117
specify the data structure that the results are returned in. By default, each row of
118
data is a tuple with one element per column from the query. However, it is also possible
119
to get results back as named tuples or dicts.
120
Tuples (Default)
121
^^^^^^^^^^^^^^^^
122
.. ipython:: python
123
with s2.connect(results_type='tuples') as conn:
124
with conn.cursor() as cur:
125
cur.execute('show variables like "auto%"')
126
for row in cur.fetchall():
127
print(row)
128
Named Tuples
129
^^^^^^^^^^^^
130
.. ipython:: python
131
with s2.connect(results_type='namedtuples') as conn:
132
with conn.cursor() as cur:
133
cur.execute('show variables like "auto%"')
134
for row in cur.fetchall():
135
print(row)
136
Dictionaries
137
^^^^^^^^^^^^
138
.. ipython:: python
139
with s2.connect(results_type='dicts') as conn:
140
with conn.cursor() as cur:
141
cur.execute('show variables like "auto%"')
142
for row in cur.fetchall():
143
print(row)
144
145