PostgreSQL and python 3.7 string search - python

I am a noob at this and appreciate all the help I can get.
Here goes:
I have a postgreSQL database that I would like to pull information out of and display the output.
I am using python 3.7 to do this.
I have connected to the database and can pull all the records and dump them on the screen.
When I try to do some logic checking, I run into problems.
Here is what I am attempting to do:
The database has two columns:
First Name and Last Name
I wanted to do a logic check, If your first name is John, print out the First and Last names.
For everyone else that is NOT named John, just print out the last name.
for row in test_database_1:
if (row[0]=='John'):
print ('Type:', row[0],':', row[1])
else:
print ('Type2:', row[0])
In the above statement, it completely skips the first print statement and just goes into the second one.
Let me know if you require additional clarification.
Thank you.

I have done some debugging and saw something odd.
When the string in row[0] is returned, it is returned as 'John |with 16 spaces|'.
Because the field in the database is set to 20, row[0] returns the name + 16 blank spaces.
This, was quite an odd find.
Regardless. Once I figured out what the heck was happening with the string being returned from the database, I was able to resolve the issue in the following way:
for row in test_database_1:
if (row[0].strip()=='John'):
print ('Type:', row[0],':', row[1])
else:
print ('Type2:', row[0])

Related

How to loop through dataframe and execute cursor to update database row by row

Hi apologies if this is a dumb question, super new to python.
Context: I have a dataframe with values that need to be updated into a table in a database. The idea is to loop through each row, pass a required value from each row to the SQL string, execute sql string.
What i have tried so far:
sales_output = merge_view[['SALES_FORECAST_ID','SALES_DATE','WEEK_NO','ADJUSTED_BASE']]
cursor = sql_connection.cursor()
for row in sales_output:
sales = row['ADJUSTED_BASE']
fid = row['SALES_FORECAST_ID']
date = row['SALES_DATE']
print('''UPDATE a SET a.WEEKLY_SALES =? FROM MA.SALES_FORECAST_VW a WHERE a.SALES_FORECAST_ID =? AND
SALES_DATE =?''',(sales,fid,date))
I am using a print statement to test if this is producing the correct SQL string to update.
This doesn't work as i get a TypeError: string indices must be, i think i understand why. you cannot select columns like that in a for loop but I'm not sure what the alternative is. I've done some googling but I think I may have confused myself.
Any help is much appreciated.
Cheers

Sqlite - How to select where "variable1" = "variable2"? [duplicate]

my question is, as the title says, how I can create a variable column in SQL lite used in Pyhton.
Here's my code, to show you what exactly I mean:
#Table with variables
def dynamic_data_entry(list_name, num, column):
element_name = list_name[num]
cur.execute("INSERT INTO myliltable (column) VALUES (?)", element_name)
con.commit()
I want to insert the column name, when I call the function, so I can use the function for more than one column, but I keep getting an error, that the column 'column' doesn't exist.
Is there another way to do this, so I can change set the column name when I call the function, or just change the SQL part.
If there is a thread about this already, I would appreciate it, if you could link it, so I can read it myself (I couldn't find anything similar to this topic yet, which worked).
Thanks in advance, I appreciate your help
You need to use the string's format() method to insert the column name into the SQL string:
cur.execute("INSERT INTO myliltable ({}) VALUES (?)".format(column),
element_name)
You can find more information in the Python docs.

Python MySQLdb returning variable amount of rows on same call

I'm working on a project but I'm kinda stuck due to a weird problem. I pull data from an external API and save it into my own SQL database with a Python script. After pulling the data I check if it's already present in my database. I do this with the following code snippet:
def getDatabaseMatchesForSummoner(summonerId):
sqlSelect = 'SELECT gameId FROM playerMatchHistory WHERE playerId=%s'
try:
cur.execute(sqlSelect,(summonerId,))
db.commit()
except MySQLdb as e:
db.rollback()
print e
gameIds = []
print cur.rowcount
for i in range(cur.rowcount):
gameIds += [str(cur.fetchone()[0])]
return gameIds
Now the problem is the following: this piece of code tends to return an amount of rows that are not in agreement with my actual database. For instance, for a particular summoner ID, it returns 7 rows whereas if I enter the query into phpMyAdmin I get 10, the correct amount. I've been searching for some hours now and I can't honestly find anything wrong with it. I tried some other things like fetchall(), other string formatting, etc. I really hope someone can point out what's wrong.

using Python to search a csv file and extract needed information

I have a large PC Inventory in csv file format. I would like to write a code that will help me find needed information. Specifically, I would like to type in the name or a part of the name of a user(user names are located in the 5th column of the file) and for the code to give me the name of that computer(computer names are located in second column in the file). My code doesn't work and I don't know what is the problem. Thank you for your help, I appreciate it!
import csv #import csv library
#open PC Inventory file
info = csv.reader(open('Creedmoor PC Inventory.csv', 'rb'), delimiter=',')
key_index = 4 # Names are in column 5 (array index is 4)
user = raw_input("Please enter employee's name:")
rows = enumerate(info)
for row in rows:
if row == user: #name is in the PC Inventory
print row #show the computer name
You've got three problems here.
First, since rows = enumerate(info), each row in rows is going to be a tuple of the row number and the actual row.
Second, the actual row itself is a sequence of columns.
So, if you want to compare user to the fifth column of an (index, row) tuple, you need to do this:
if row[1][key_index] == user:
Or, more clearly:
for index, row in rows:
if row[key_index] == user:
print row[1]
Or, if you don't actually have any need for the row number, just don't use enumerate:
for row in info:
if row[key_index] == user:
print row[1]
But that just gets you to your third problem: You want to be able to search for the name or a part of the name. So, you need the in operator:
for row in info:
if user in row[key_index]:
print row[1]
It would be clearer to read the whole thing into a searchable data structure:
inventory = { row[key_index]: row for row in info }
Then you don't need a for loop to search for the user; you can just do this:
print inventory[user][1]
Unfortunately, however, that won't work for doing substring searches. You need a more complex data structure. A trie, or any sorted/bisectable structure, would work if you only need prefix searches; if you need arbitrary substring searches, you need something fancier, and that's probably not worth doing.
You could consider using a database for that. For example, with a SQL database (like sqlite3), you can do this:
cur = db.execute('SELECT Computer FROM Inventory WHERE Name LIKE %s', name)
Importing a CSV file and writing a database isn't too hard, and if you're going to be running a whole lot of searches against a single CSV file it might be worth it. (Also, if you're currently editing the file by opening the CSV in Excel or LibreOffice, modifying it, and re-exporting it, you can instead just attach an Excel/LO spreadsheet to the database for editing.) Otherwise, it will just make things more complicated for no reason.
enumerate returns an iterator of index, element pairs. You don't really need it. Also, you forgot to use key_index:
for row in info:
if row[key_index] == user:
print row
it's hard to tell what's wrong without knowing how your file looks like, but I'm pretty sure the error is:
for row in info:
if row[key_Index] == user: #name is in the PC Inventory
print row #show the computer name
where you did define the column, but forget to get that column from each line you're comparing to the user, so in the end you're comparing a string with a list.
And you don't need the enumerate, per default you iterate over the rows.

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