Fetching data from database in a loop - mysql

Is there a logical way to get the data from sql periodically? I want to fetch a row from a table in my postgres DB, run some calculations on it in my python script, and fetch the second row of the data and go on with this procedure till all the calculation has been done.
enter image description here
In the table above, I would like to fetch each array of the travel time, do the calculations on it in my python code, and then go to the 2nd row and so on.

I believe your database library will provide you with a method for doing this. It might be called a cursor, a pointer or an iterator, which will have a method for fetching the next row from a query.
From https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html
cursor.execute("SELECT * FROM employees")
row = cursor.fetchone()
while row is not None:
print(row)
row = cursor.fetchone()

Related

Power Automate Desktop - Convert a data table (with multiple rows) to JSON

I've been researching the best way to convert a data table from excel (with multiple rows) to JSON.
I found a solution on here that appears to "mostly" work, but I am not familiar with JSON to know if it's converting multiple rows correctly.
Here is the data table that I am starting with (from excel)
Here are the steps I took to convert this to JSON
Step 1: Set variable called INVObject to be empty to initialize it
Step 3: Added a For each to loop through each Data Row in the Data Table
Step 4: Added a Set Variable to set the INVObject (Custom Object) to the Data Table for each loop in the For each
Step 5: Convert the Custom Object INVObject to JSON
Results: There is one row/object with all 3 rows from the Data table on the same row
If you scroll to the right, the 2nd row eventually starts and then the 3rd row.
I was expecting to see 3 lines/rows/object to represent the 3 different rows in the Data table.
Can someone provide some insight as to if I am doing something wrong or if this is the expected results for multiple rows?
Thank You!
There is an option in Actions under Variables: 'Convert Custom Object to JSON'
https://learn.microsoft.com/en-us/power-automate/desktop-flows/actions-reference/variables#convertcustomobjecttojson

Dataframe is of type 'nonetype'. How should I alter this to allow merge function to operate?

I have pulled in data from a number of csv files, as well as a database. I wish to use a merge function to make a dataframe isolating the phone numbers that are contained in both dataframes(one originating from csv, the other originating from the database). However, the dataframe from the database displays as type 'nonetype.' This disallows any operation such as merge. How can i change this to allow the operation?
The data comes in from the database as a list of tuples. I then convert this to a dataframe. However, as stated above, it displays as 'nonetype.' I'm assuming at the moment I am confused about about how dataframes handle data types.
#Grab Data
mycursor = mydb.cursor()
mycursor.execute("SELECT DISTINCT(Cell) FROM crm_data.ap_clients Order By Cell asc;")
apclients = mycursor.fetchall()
#Clean Phone Number Data
for index, row in data.iterrows():
data['phone_number'][index] = data['phone_number'][index][-10:]
for index, row in data2.iterrows():
data2['phone_number'][index] = data2['phone_number'][index][-10:]
for index, row in data3.iterrows():
data3['phone_number'][index] = data3['phone_number'][index][-10:]
#make data frame from csv files
fbl = pd.concat([data,data2,data3], axis=0, sort=False)
#make data frame from apclients(database extraction)
apc = pd.DataFrame(apclients)
#perfrom merge finding all records in both frames
successfulleads= pd.merge(fbl, apc, left_on ='phone_number', right_on='0')
#type(apc) returns NoneType
The expected results are to find all records in both dataframes, along with a count so that I may compare the two sets. Any help is greatly appreciated from this great community :)
So it looks like I had a function to rename the column of the dataframe as shown below:
apc = apc.rename(columns={'0': 'phone_number'}, inplace=True)
for col in apc.columns:
print(col)
the code snippet out of the above responsible:
inplace=True
This snippet dictates whether or not the object is modified in the dataframe, or whether a copy is made. The return type on said object is of nonetype.
Hope this helps whoever ends up in my position. A great thanks again to the community. :)

App inventor 2 App: RowID Fusion Table

With App inventor 2 I got the ROWID number and I put the value in the "riga" variable. The Query is executing once and it writes correctely the data in the fusion table row, but then the query keeps on executing many times and the app writes hundred of times "400 Bad Request: Rowid must contains only numerals and underscores".
Why is it happening?
you created an endless loop
you should add a variable let's name it action, set it to rowid while you get the rowid and set it to update while you do the update
in the fusiontable controls got result event you need an if statement like this (pseudocode)
if action = "rowid"
then extract the received rowid and do the update
else print "update was successful"
btw. to get the rowid, you better split the result at \n (new line) to convert it into a list with 2 items, then select the second item to get the row id

How to display multiple records in java?

I have connected to one MySql table to fetch 1000 records that need to be displayed using JSP. I want to split my table into multiple tables (of size 50) which can be viewed by using next button on jsp page. How can I implement it?
String sql = "select * from people";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet result = statement.executeQuery();
while(result.next()) { // here i get 1000 records. how can i display these records on mulitple jsp pages?
// ... get column values from this record
}
Above gives 1000 records and I use a PreparedStatement.
Assuming you can fetch the complete table in JSP.
Just add one more param to the function returning the ROWS COUNTER
Add COUNTER to LIMIT clause in your QUERY.
Now you need to keep track of the COUNTER so Update the URL on click on next as
First Click - URL?pageCOUNTER=1
Second Click - URL?pageCOUNTER=2
Fetch the pageCOUNTER varaible from URL and pass it to your function as COUNTER
And so on. Keep getting you result.
OR USE JAVASCRIPT TABLE - http://www.datatables.net/

rails run function inside query

If I have the following query, is it possible to be able to run a function inside? Let's say I want to add WHERE zip_code = user_distance(zip_code)?
I want to take data from each row and run it through a function before actually selecting it.
#posts = Listing.find_by_sql(["SELECT * FROM listings WHERE industry = ? && ", current_user.industry])
If you are mainly looking to get this working and not worrying so much about performance (because going straight to the SQL is faster than going through ActiveRecord) then you could do:
listings = []
Listing.all.each do |listing|
listings << listing if user_distance(listing.zip_code)
end
So, it will go through each listing and add it to that array if the user_distance method returns true (or however it is set up).
Another thing you could do is set up a stored procedure ("stored proc") on your database that takes in a zip code and returns what it is you want (i.e, does the same thing as user_distance), and that user defined variable max_distance could be in a database table so it's accessible to your stored procedure. Then you could call that stored proc from the SQL and still be able to pass in the zip_code of each row.