Hello,
A customer of mine has set up a SQL Server table with address, latitude, and longitude fields. They've asked me to geocode the addresses and populate the latitude and longitude fields with the results. This is a non-spatial table. They plan to use the lat/long values in other, non-ESRI, applications.
I've already got a process that will read the addresses from the SQL Server table, geocode them, and save the results into a feature class within a file geodatabase. I'm trying to build a python script that will update the SQL Server table with the results from the feature class.
<SPAN class="keyword token">import</SPAN> pyodbc
<SPAN class="keyword token">import</SPAN> arcpy
fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\FGDB.gdb\featureclass'</SPAN>
cnxn <SPAN class="operator token">=</SPAN> pyodbc<SPAN class="punctuation token">.</SPAN>connect<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Driver={ODBC Driver 17 for SQL Server};'</SPAN>
<SPAN class="string token">'Server=SQLServer;'</SPAN>
<SPAN class="string token">'Database=Database;'</SPAN>
<SPAN class="string token">'Trusted_connection=yes'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'FCID'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'GCAcc'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> curFC<SPAN class="punctuation token">:</SPAN>
curSQL<SPAN class="operator token">=</SPAN>cnxn<SPAN class="punctuation token">.</SPAN>cursor<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
curSQL<SPAN class="punctuation token">.</SPAN>execute<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'UPDATE tabAddress SET Addr1_GCAcc = ? WHERE TableID = ?'</SPAN><SPAN class="punctuation token">,</SPAN> curFC<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> curFC<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
curSQL<SPAN class="punctuation token">.</SPAN>commit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
curSQL<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>FYI, I changed the feature class and SQL server names in the above code. However, I was able to successfully read the SQL server table using the pyodbc connection.
After I run the script, I get the following error message:
Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
File <SPAN class="string token">"C:\Users\myprofile\Desktop\testODBC\asdf.py"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">14</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN>
curSQL<SPAN class="punctuation token">.</SPAN>execute<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'UPDATE tabAddress SET Addr1_GCAcc = ? WHERE TableID = ?'</SPAN><SPAN class="punctuation token">,</SPAN> curFC<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> curFC<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
SystemError<SPAN class="punctuation token">:</SPAN> error <SPAN class="keyword token">return</SPAN> without exception set<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
FYI, FCID is a long integer field and GCAcc is a string field. Could the FCID field in the execute statement not be formatting correctly? I checked with the SQL Server admin and I'm supposed to have update capabilities.
Thank you,
Lucas Murray