|
| 1 | +Querying Using PyExasol |
| 2 | +========================================= |
| 3 | + |
| 4 | +In this section, we will create a table and insert data inside that table using PyExasol. |
| 5 | +The schema we are working with in this example is named 'SCHEMA'. You can replace it with your own schema name. |
| 6 | + |
| 7 | + |
| 8 | +Creating a Table |
| 9 | +---------------------- |
| 10 | + |
| 11 | +Using the **execute** method of the connection object we created earlier in the :ref:`installation-pyexasol` section. |
| 12 | +We are creating a table called **Employee**. |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | +
|
| 16 | + create_table = connection.execute("CREATE OR REPLACE TABLE SCHEMA.Employee(employee_id INT PRIMARY KEY, " \ |
| 17 | + "name VARCHAR(50), " \ |
| 18 | + "hire_date DATE)") |
| 19 | +
|
| 20 | +For more information about the **execute** method. Check out `ExaConnection.execute class <https://exasol.github.io/pyexasol/master/_modules/pyexasol/connection.html#ExaConnection.execute>`_. |
| 21 | + |
| 22 | + |
| 23 | +Inserting Data into the Table |
| 24 | +------------------------------------ |
| 25 | + |
| 26 | +Using Pandas DataFrame |
| 27 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 28 | + |
| 29 | +This is an example of inserting values into the "EMPLOYEE" table using pandas dataframe. |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + data = { |
| 34 | + "employee_id": [15, 20, 30, 40, 50], |
| 35 | + "name": ['John', 'Jane', 'Michael', 'Emily', 'David'], |
| 36 | + "hire_date": ['2023-01-15', '2022-07-10', '2021-03-05', '2023-06-20', '2020-11-12'], |
| 37 | + } |
| 38 | + df_employees = pd.DataFrame(data) |
| 39 | + connection.import_from_pandas(df_employees, ('SCHEMA', 'EMPLOYEE')) |
| 40 | +
|
| 41 | +For custom params check out `ExaConnection.import_from_pandas <https://exasol.github.io/pyexasol/master/api.html#pyexasol.ExaConnection.import_from_pandas>`_ |
| 42 | + |
| 43 | +Using a file |
| 44 | +^^^^^^^^^^^^^^ |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + inserting_a_file = connection.import_from_file('file.csv', ('SCHEMA','EMPLOYEE')) |
| 49 | +
|
| 50 | +Make sure to that your file has the same column names as your table. |
| 51 | + |
| 52 | +There are multiple options to import data from using PyExasol. Check out more options from `here <https://exasol.github.io/pyexasol/master/api.html#pyexasol.ExaConnection.import_from_callback>`_ |
| 53 | + |
| 54 | + |
| 55 | +Retrieving Data From the Database |
| 56 | +------------------------------------ |
| 57 | + |
| 58 | +Like importing there are multiple options of **retrieving** data using PyExasol. |
| 59 | +Here is an example of exporting data into a pandas dataframe. |
| 60 | + |
| 61 | +.. code-block:: python |
| 62 | +
|
| 63 | + retrieve_into_pandas = connection.export_to_pandas('SELECT * FROM SCHEMA.EMPLOYEE') |
| 64 | + print(retrieve_into_pandas.shape) |
| 65 | +
|
0 commit comments