Python: Iterating through q.collections.table - python

I used Dan Nugents' Python library (http://www.timestored.com/kdb-guides/python-api) and successfully got the table object, i.e., after submitting a query to the database. However, how can I iterate through every single row of the object?
Let's say the table object is "results". I tried:
for i in results.keys():
print results[i]
However, it says "raise ValueError". Any ideas?

From this section (4. Example of sending / receiving data and tables. of this). You seem to be getting a list of dict's(results is not a dictionary). Try printing them as
for result in results:
print result

Related

Python: Filter out rows from result set of pyodbc.row when row contains string

I have not been able to find an answer to this seemingly straightforward filter process.
I have a result set of table names for a simple odbc query and I want to filter that result set anything that contains the prefix 'wer_'
*Some pyodbc connection code*
cursor.execute(<SQL statement which gets the list of tables>)
results = cursor.fetchall()
results = [key for key in results if str(key.name).str.contains('wer_')]
^ I've tried various methods around this but so far no dice. Can you help?
It turned out to be fairly straight forward in the end. It seems pyodbc.row has an attribute of name which you can compare against
*Some pyodbc connection code*
cursor.execute(<SQL statement which gets the list of tables>)
results = cursor.fetchall()
results = [key for key in results if 'wer_' not in key.name]
I hope this helps somebody in the future!

SQL Alchemy return type

I am querying the table using session.query().filter. I get an output like:
(u'1234'). But when I try to translate this using str.translate(None, '5), it throws an error
KeyedTuple' object has no attribute 'translate'
Where am I going wrong?
You get a tuple with one element. You have to retrieve this one element first before using result_string.translate on it.
If you save the return of your session.query().filter inside a variable result do
if result: # Check that result is not empty
result[0].translate(...)

Python - string indices must be integers

I apologize if this is a basic fix and the length of the post, but I am new to Python. I've also included a large chunk of the script for context purposes.
I am using a script to pull scanning data from JSON into a MySQL DB. The script was working fine until an update was released.
Now when I run the script I receive the error:
for result in resultc['response']['results']:
TypeError: string indices must be integers
Before this update I knew the data types for each value, but this has changed and I cannot pinpoint where. Is there a way to convert each value to be recognized as a string?
# Send the cumulative JSON and then populate the table
cumresponse, content = SendRequest(url, headers, cumdata)
resultc = json.loads(content)
off = 0
print "\nFilling cumvulndata table with vulnerabilities from the cumulative database. Please wait..."
for result in resultc['response']['results']:
off += 1
print off, result
cursor.execute ("""INSERT INTO cumvulndata(
offset,pluginName,repositoryID,
severity,pluginID,hasBeenMitigated,
dnsName,macAddress,familyID,recastRisk,
firstSeen,ip,acceptRisk,lastSeen,netbiosName,
port,pluginText,protocol) VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,(FROM_UNIXTIME(%s)),%s,%s,(FROM_UNIXTIME(%s)),%s,%s, %s,%s)""" , (off,result["pluginName"],result["repositoryID"]),result["severity"]),
result["pluginID"]), result["hasBeenMitigated"]),result["dnsName"],
result["macAddress"],result["familyID"]),result["recastRisk"]),
result["firstSeen"],result["ip"],result["acceptRisk"],result["lastSeen"],
result["netbiosName"],result["port"],result["pluginText"],result["protocol"]))
Put this before the for loop to work out which object is the string (I guess it's probably the second one)
print type(resultc)
print type(resultc['response'])

python SPARQL query RESULTS BINDINGS: IF statement on BINDINGs value?

Using Python with SPARQLWrapper, JSON, urlib2 & cgi. Had trouble passing a working SPARQL query with some NULL values to python so I populated the blanks with a literal and will try to filter at the output. I have this results section example:
for result in results["results"]["bindings"]:
project = result["project"]["value"].encode('utf-8')
filename = result["filename"]["value"].encode('utf-8')
url = result["url"]["value"].encode('utf-8')
...and I print the %s. Is there a way to filter a value, i.e., IF VALUE NE "string" then PRINT? Or is there another workaround? I'm at the tail-end of a small project, I know I need a better wrapper, I just need to get these results filtered before I can move on. T very much IA...
I'm one of the developers of the SPARQLWrapper library, and the question had been already answered at the mailing list.
Regarding optionals values on the original query, the result set will come with no values for those variables. The problems is that we'd need to parse the query to populate such missing entries, and we want to avoid such parsing; therefore you'd need to check it for avoiding runtime problems with KeyError.
Usually I use a code like:
for result in results["results"]["bindings"]:
party = result["party"]["value"] if ("party" in result) else None

Python: using pyodbc and replacing row field values

I'm trying to figure out if it's possible to replace record values in a Microsoft Access (either .accdb or .mdb) database using pyodbc. I've poured over the documentation and noted where it says that "Row Values Can Be Replaced" but I have not been able to make it work.
More specifically, I'm attempting to replace a row value from a python variable. I've tried:
setting the connection autocommit to "True"
made sure that it's not a data type issue
Here is a snippet of the code where I'm executing a SQL query, using fetchone() to grab just one record (I know with this script the query is only returning one record), then I am grabbing the existing value for a field (the field position integer is stored in the z variable), and then am getting the new value I want to write to the field by accessing it from an existing python dictionary created in the script.
pSQL = "SELECT * FROM %s WHERE %s = '%s'" % (reviewTBL, newID, basinID)
cursor.execute(pSQL)
record = cursor.fetchone()
if record:
oldVal = record[z]
val = codeCrosswalk[oldVal]
record[z] = val
I've tried everything I can think bit cannot get it to work. Am I just misunderstanding the help documentation?
The script runs successfully but the newly assigned value never seems to commit. I even tried putting "print str(record[z])this after the record[z] = val line to see if the field in the table has the new value and the new value would print like it worked...but then if I check in the table after the script has finished the old values are still in the table field.
Much appreciate any insight into this...I was hoping this would work like how using VBA in MS Access databases you can use an ADO Recordset to loop through records in a table and assign values to a field from a variable.
thanks,
Tom
The "Row values can be replaced" from the pyodbc documentation refers to the fact that you can modify the values on the returned row objects, for example to perform some cleanup or conversion before you start using them. It does not mean that these changes will automatically be persisted in the database. You will have to use sql UPDATE statements for that.

Categories

Resources