Path: blob/main/docs/_sources/getting-started.rst.txt
469 views
.. currentmodule:: singlestoredb1.. ipython:: python2:suppress:3import singlestoredb as s24conn = s2.connect()5Getting Started6===============7Database connections can be made using either keyword parameters or8a URL as described in the following sections.9Connect using DB-API Parameters10-------------------------------11Connections to SingleStoreDB can be made using the parameters described in12the `Python DB-API <https://peps.python.org/pep-0249/>`_. The ``host=``13parameter can be either a hostname or IP address (it can also be a14URL as shown in the following section). The ``port=`` parameter is an15integer value of the database server port number. The ``user=`` and16``password=`` parameters specify the database user credentials. The17``database=`` parameter, optionally, specifies the name of the database18to connect to.19A full list of connection parameters can be seen in the API documentation20for the :func:`singlestoredb.connect` function.21.. ipython:: python22:verbatim:23import singlestoredb as s224conn = s2.connect(host='...', port='...', user='...',25password='...', database='...')26Connect using a URL27-------------------28In addition, you can user a URL like in the SQLAlchemy package.29.. ipython:: python30:verbatim:31conn = s2.connect('user:password@host:port/database')32URLs work equally well to connect to the33`Data API <https://docs.singlestore.com/managed-service/en/reference/data-api.html>`_.34.. ipython:: python35:verbatim:36conn = s2.connect('https://user:password@host:port/database')37Specifying Additional Connection Parameters38-------------------------------------------39Connection parameters can be set either in the URL or40as parameters. Here ``local_infile=`` is set as a URL parameter.41.. ipython:: python42:verbatim:43conn = s2.connect('https://user:password@host:port/database?local_infile=True')44In this example, ``local_infile=`` and user credentials are specified45as keyword parameters.46.. ipython:: python47:verbatim:48conn = s2.connect('https://host:port/database', user='...', password='...',49local_infile=True)50Executing Queries51-----------------52Once you have a connection established, you can query the database.53As defined in the DB-API, a cursor is used to execute queries and fetch54the results.55.. ipython:: python56with conn.cursor() as cur:57cur.execute('show variables like "auto%"')58for row in cur.fetchall():59print(row)60Parameter Substitution61......................62If your queries require parameter substitutions, they can be specified in63one of two formats: named (``%(name)s``) or positional (``%s``).64.. warning:: As of v0.5.0, the substition parameter has been changed from65``:1``, ``:2``, etc. for list parameters and ``:foo``, ``:bar``, etc.66for dictionary parameters to ``%s`` and ``%(foo)s``, ``%(bar)s``, etc.67respectively, to ease the transition from other MySQL Python packages.68Named Substitution69^^^^^^^^^^^^^^^^^^70When named parameters71are used, the data structure passed to the :meth:`Cursor.execute` method72must be a dictionary, where the keys map to the names given in the substitutions73and the values are the values to substitute.74In the example below, ``%(pattern)s`` is replaced with the value ``"auto%"``. All75escaping and quoting of the substituted data values is done automatically.76.. ipython:: python77with conn.cursor() as cur:78cur.execute('show variables like %(pattern)s', dict(pattern='auto%'))79for row in cur.fetchall():80print(row)81Positional Substitution82^^^^^^^^^^^^^^^^^^^^^^^83If positional parameters are used, the data structure passed to the84:meth:`Cursor.execute` method must be a list or tuple with the same85number of elements as there are ``%s`` values in the query string.86In the example below, ``%s`` is replaced with the value ``"auto%"``. All87escaping and quoting of the substituted data values is done automatically.88.. ipython:: python89with conn.cursor() as cur:90cur.execute('show variables like %s', ['auto%'])91for row in cur.fetchall():92print(row)93Fetching Results94----------------95Fetching results can be done in a number of ways. The DB-API specifies three methods96that can be used to fetch results: :meth:`Cursor.fetchone`, :meth:`Cursor.fetchall`,97and :meth:`Cursor.fetchmany`.98The :meth:`Cursor.fetchone` method fetches a single row of data returned by a query.99The :meth:`Cursor.fetchall` method fetches all of the results of a query. The100:meth:`Cursor.fetchmany` method fetches a specified number of rows to retrieve.101The choice of which one to use depends mostly on the expected size of the result.102If the result is expected to be fairly small, fetching the entire result in one103call may be fine. However, if the query result will be large enough to put a strain104on the client computer's memory, it may be a better idea to fetch smaller batches105using :meth:`Cursor.fetchmany`.106In additon to the DB-API methods for fetching results, a :class:`Cursor` can be107iterated over itself.108.. ipython:: python109with conn.cursor() as cur:110cur.execute('show variables like "auto%"')111for row in cur:112print(row)113Result Type114...........115In addition to being able to specify the amount of data to be retrieved, you can also116specify the data structure that the results are returned in. By default, each row of117data is a tuple with one element per column from the query. However, it is also possible118to get results back as named tuples or dicts.119Tuples (Default)120^^^^^^^^^^^^^^^^121.. ipython:: python122with s2.connect(results_type='tuples') as conn:123with conn.cursor() as cur:124cur.execute('show variables like "auto%"')125for row in cur.fetchall():126print(row)127Named Tuples128^^^^^^^^^^^^129.. ipython:: python130with s2.connect(results_type='namedtuples') as conn:131with conn.cursor() as cur:132cur.execute('show variables like "auto%"')133for row in cur.fetchall():134print(row)135Dictionaries136^^^^^^^^^^^^137.. ipython:: python138with s2.connect(results_type='dicts') as conn:139with conn.cursor() as cur:140cur.execute('show variables like "auto%"')141for row in cur.fetchall():142print(row)143144145