<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Notebooks and SQL Servers in ArcGIS Notebooks Questions</title>
    <link>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1062726#M314</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm currently using FME Desktop to extract data from an SQL server and then amend a AGOL feature service, to ensure the feature service is updated. While this works, its a manual process and that limits how often the feature service can be updated.&lt;/P&gt;&lt;P&gt;I was hoping that there was a way that I can use Notebooks instead of FME Desktop, and also take advantage of the scheduling functionality to update the feature more often.&lt;/P&gt;&lt;P&gt;I'm slowly learning Python, but wanted to check whether Notebooks would be able query an SQL database.&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
    <pubDate>Fri, 28 May 2021 04:36:06 GMT</pubDate>
    <dc:creator>ToryChristensen</dc:creator>
    <dc:date>2021-05-28T04:36:06Z</dc:date>
    <item>
      <title>Notebooks and SQL Servers</title>
      <link>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1062726#M314</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm currently using FME Desktop to extract data from an SQL server and then amend a AGOL feature service, to ensure the feature service is updated. While this works, its a manual process and that limits how often the feature service can be updated.&lt;/P&gt;&lt;P&gt;I was hoping that there was a way that I can use Notebooks instead of FME Desktop, and also take advantage of the scheduling functionality to update the feature more often.&lt;/P&gt;&lt;P&gt;I'm slowly learning Python, but wanted to check whether Notebooks would be able query an SQL database.&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 04:36:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1062726#M314</guid>
      <dc:creator>ToryChristensen</dc:creator>
      <dc:date>2021-05-28T04:36:06Z</dc:date>
    </item>
    <item>
      <title>Re: Notebooks and SQL Servers</title>
      <link>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1062732#M315</link>
      <description>&lt;P&gt;Hi Tory&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have had this question for a long time.&lt;/P&gt;&lt;P&gt;We are an organization purely on AGOL. There hasn't been ways to schedule notebooks to run.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We adopted Azure as a cloud computing system and things changed. Within Azure, I can create Functions.&lt;/P&gt;&lt;P&gt;Functions are triggered by occurrence of time or new data. I integrated ArcGIS python API python scripts, scipy spatial and QGIS and I can now be able to directly read and update feature services on AGOL using SQL servers on Premise and Azure SQL dbs in cloud. Importantly, I can use databricks which is scalable and can handle million of rows of data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know you may not have these facilities, however it may not possible to do this within ArcGIS.&amp;nbsp; &amp;nbsp;I believe other cloud computing platforms other than Azure are capable of doing the same.&lt;/P&gt;&lt;P&gt;I am finding it quite limited to rely on ArcPro to integrate GIS datasets with non GIS data and beginning to ask myself if as GIS specialist I should look beyond the conventional GIS software.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 05:20:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1062732#M315</guid>
      <dc:creator>wwnde</dc:creator>
      <dc:date>2021-05-28T05:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Notebooks and SQL Servers</title>
      <link>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1062843#M317</link>
      <description>&lt;P&gt;The answer: it depends!&lt;/P&gt;&lt;P&gt;Is your SQL server accessible via the web, or do you need to be in-network? If the former, you can absolutely use a Notebook. If the latter, you'll need a local Python file, but you can still use a Notebook environment to develop the script.&lt;/P&gt;&lt;P&gt;In either case, you'll want to take a look at &lt;A href="https://pandas.pydata.org/" target="_self"&gt;pandas&lt;/A&gt;, specifically &lt;A href="https://pandas.pydata.org/docs/reference/api/pandas.read_sql.html#pandas.read_sql" target="_self"&gt;pandas.read_sql()&lt;/A&gt;, which can read either a whole table or a specific query into a dataframe. For those not familiar, pandas (and the underlying numpy) is a &lt;EM&gt;fantastic&lt;/EM&gt; tool for analyzing and manipulating tabular data.&lt;/P&gt;&lt;P&gt;Connecting to an SQL database via pandas utilizes &lt;A href="https://docs.sqlalchemy.org/en/13/core/connections.html" target="_self"&gt;sqlalchemy&lt;/A&gt;, so that would be useful to look at as well, though the pandas examples show you everything you need to know.&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;Side note: if you're working in the AGOL Notebook environment, you will be missing some of the submodules sqlalchemy needs to connect to various database types. These can be handled by including a cell which says:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px lia-align-left"&gt;&lt;FONT face="simsun,hei"&gt;pip install pymssql &lt;/FONT&gt;(or other required submodule)&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px lia-align-left"&gt;If you're working locally, you can have this installed already, and skip that step.&lt;/P&gt;&lt;P&gt;Once you've queried out the data and made whatever changes you need, you can then use the &lt;A href="https://developers.arcgis.com/python/api-reference/" target="_self"&gt;ArcGIS Python API&lt;/A&gt; to append to / update / overwrite hosted feature services.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS
import pandas as pd
from sqlalchemy import create_engine

# DB connection
engine = create_engine('mssql+pymssql://*server_name*/*database_name*?trusted_connection=yes')

# Query string
query = """SELECT t.somecolumn, t.othercolumn
    FROM dbo.TableName t
    ORDER BY t.somecolumn DESC"""

# Query DB to dataframe
df = pd.ready_sql(query, engine)

## Do some things to the dataframe here

# Connect to your AGOL / Enterprise
gis = GIS('org-url', 'username', 'password')

# Get the target layer; swap the '0' for whatever layer index you need
# Alternate: use .tables[0] if target is non-spatial
target_layer = gis.content.get('layer itemID').layers[0]

# Clear existing records
target_layer.manager.truncate()

# Append new records from dataframe
target_layer.edit_features(adds=df.spatial.to_featureset())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I should say, there are LOTS of ways to manage that end part. Maybe instead of truncate / append, you choose to identify updated rows based on a unique identifier column. Maybe you're just appending new data and you don't need to truncate anything. That part will really depend on your particular situation.&lt;/P&gt;&lt;P&gt;Now, if you're working locally, you can use a Notebook to develop the script, as I mentioned. Once you're sure it works, start to finish, with no manual intervention, copy all the code blocks into a single &lt;STRONG&gt;layer_&lt;/STRONG&gt;&lt;STRONG&gt;update.py&lt;/STRONG&gt; script, or whatever you want to call it. Then you can schedule that script to run using your computer's task scheduler.&lt;/P&gt;&lt;P&gt;If you're working in an AGOL Notebook, you can schedule it to run from there.&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 13:35:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1062843#M317</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-05-28T13:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Notebooks and SQL Servers</title>
      <link>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1063953#M318</link>
      <description>&lt;P&gt;Thank you for such a detailed response. I'm starting to work on it now!!!&lt;/P&gt;&lt;P&gt;I'll let you know how I go &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jun 2021 05:33:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-notebooks-questions/notebooks-and-sql-servers/m-p/1063953#M318</guid>
      <dc:creator>ToryChristensen</dc:creator>
      <dc:date>2021-06-02T05:33:29Z</dc:date>
    </item>
  </channel>
</rss>

