| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 119206 | 2011-07-10 23:59:00 | Execute SQL command from Python | Mike (15) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 1215921 | 2011-07-10 23:59:00 | I have a python script that does a number of different things, but one of the first things it needs to do is find a record in a particular SQL (MS SQL) table, and if it has a value in a specific field, to remember that value (python variable), set the value to Null, run the rest of the python, then find that record again and put the value back into that field. I have an existing script that returns values based on a SQL query (see code below), but in this case I want to actually make changes in SQL based on the query results. import _mssql as sql archives = [] #Array for archive tables conn = sql.connect(server='server', user='user', password='password', database='database') #Connect to SQL conn.execute_query("SELECT * FROM sys.tables where name like '%[_]H' order by name") #SQL query for listing Archive tables (end in _H) for row in conn: ...Any ideas on how to do what I want? Thanks, Mike. |
Mike (15) | ||
| 1215922 | 2011-07-11 00:03:00 | I should probably add that my existing script is unrelated :) it just shows how I am currently querying SQL from python Mike. |
Mike (15) | ||
| 1215923 | 2011-07-11 04:29:00 | You need to just execute the SQL UPDATE command. UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value |
MushHead (10626) | ||
| 1215924 | 2011-07-12 21:31:00 | Thanks MushHead - it wasn't the update statement I had issues with, it was the python part. What I needed was a conn.execute_non_query (simple as that) :) Cheers, Mike. |
Mike (15) | ||
| 1 | |||||