Saturday, January 26, 2013

Re: Python Cloud SQL Search Example?

Hi

How do I manage two different "cursors" in SQL Cloud?

I have one cursor moving through a database checking for certain data.
When there is data missing I want to add new data and then continue
moving through the data. How do I add data using a new cursor?

# Check if Station exists
sqlString = "SELECT * FROM weather WHERE weather_code = '"
+ station_id + "' "
outputString += "<tr><td colspan=8>" + sqlString + "</td></tr>"

cursor.execute(sqlString)

data = cursor.fetchall()
if len(data) == 0:
outputString += "<tr><td colspan=8>Need to Add Data</td></tr>"

cursor.execute('INSERT INTO metar (metar_data) VALUES
(%s)', (MySQLdb.escape_string(mdata)))
conn.commit()

Do I use cursor.execute here? or cursor2.execute?

# weather_id = <no idea>.lastrowid
# outputString += "<tr><td colspan=8>Data Exists ID: " +
str(weather_id) + "</td></tr>"
else:
weather_id = data[0][0]
# outputString += "<tr><td colspan=8>Data Exists ID: " +
str(weather_id) + "</td></tr>"

Best regards

Andre F Bruton






On Sat, Jan 26, 2013 at 8:07 PM, Andre Bruton <andrebruton@gmail.com> wrote:
> Thank you. Yes this did help.
>
> I did a search on fetchall() and found a great example at:
>
> http://stackoverflow.com/questions/2440147/how-to-check-the-existence-of-a-row-in-sqlite-with-python
>
> Best regards
>
> Andre
>
>
> On Saturday, January 26, 2013 5:33:47 PM UTC+2, Razvan Musaloiu-E. wrote:
>>
>> Some nice examples are in the "MySQLdb module" section from:
>>
>> http://zetcode.com/db/mysqlpython/
>>
>> The .fetchall()/.fetchone() will returns an empty tuple if the query
>> doesn't return any rows:
>>
>> Sample code:
>>
>> con = rdbms.connect(instance=...)
>> cur = con.cursor()
>> cur.execute("CREATE TABLE IF NOT EXISTS test.t(i int)")
>> cur.execute("SELECT * FROM test.t")
>> print cur.fetchall()
>>
>> cur.execute("SELECT * FROM test.t")
>> print cur.fetchall()
>>
>>
>>
>> Output:
>>
>> ()
>> ()
>>
>>
>> -- Razvan ME
>>
>>
>> On Sat, Jan 26, 2013 at 6:59 AM, Andre Bruton <andre...@gmail.com> wrote:
>>>
>>> Hi
>>>
>>> Google gives an example of how to add and list data from a Cloud SQL
>>> database, and that is running great.
>>>
>>> How would I search for a record using Python? I want to search for a
>>> record and do something if there is a record and do something else if there
>>> is no record.
>>>
>>> Does anyone have a working example that I can look at?
>>>
>>> Best regards
>>>
>>> Andre F Bruton
>>>
>>>
>>> --
>>>
>>>
>>
>>
> --
>
>

No comments:

Post a Comment