Thank you for your donation!


Connecting to database with python
#1
I'm on Moode 4.1+Rpi2b and I have just upgraded to Moode 6.5.2 + Rpi4 I run a python script that helps me with power and a button and the script has a few places, where I'm unsure if I need to change anything.

First up is checking if Airplay is on:

Code:
# Airplay state check
def IsAirplayOn():
    
    global ButtonOverride
    
    AirplayOn = False
    try:
        cur = con.cursor()    
        cur.execute('select value from cfg_system where param=\'airplayactv\'')
        data = cur.fetchone()[0]    
        if (Debug): print "SQL = %s" % data
        if data == "1":
            AirplayOn = True

    except SQLite3.Error, e:
        if (Debug): print "Error %s:" % e.args[0]

    if ButtonOverride:
        AirplayOn = False
    
    if Debug: print "Returning IsAirplayOn = {0}".format(AirplayOn)
    return AirplayOn

and

Code:
# Connect to player database, in order to read Airplay status     
try:
        con = SQLite3.connect('/var/local/www/db/moode-sqlite3.db')    
except SQLite3.Error, e:
        if Debug: print "Error %s:" % e.args[0]
        sys.exit(1)

Do I need to change anything here when upgrading?

The last bit of code, that I'm unsure about is this:

Code:
# Setup MDPClient and connect to MPD on localhost
Client = mpd.MPDClient() # create client object
Client.timeout = None
Client.idletimeout = None

while True:
    try:
        Client.connect("localhost", 6600) # connect to localhost:6600
    except:
        continue
    break

Does anyone know if I need to change anything in these three code bits in order to get it woring under moode 6.5.2 ?
Max Schmeling
Reply
#2
@Max Schmeling

What, no "try it and see" test  Tongue

The airplayactv parameter hasn't changed in 6.5.2. I think your code will work unmodified.

However, you ought to consider updating it to Python3. At some point the Debian maintainers will remove the Python 2 stack and I expect the Raspbian maintainers will too.

Regards,
Kent
Reply
#3
(05-18-2020, 04:55 PM)TheOldPresbyope Wrote: @Max Schmeling

What, no "try it and see" test  Tongue

The airplayactv parameter hasn't changed in 6.5.2. I think your code will work unmodified.

However, you ought to consider updating it to Python3. At some point the Debian maintainers will remove the Python 2 stack and I expect the Raspbian maintainers will too.

Regards,
Kent

@TheOldPresbyope 

Thanks. Smile  I just tried updating to P3 and installing python-mpd2. Everything seems to work, but I'll have to check, once I get it in the box with the buttons. I'll report back Smile
Max Schmeling
Reply
#4
(05-19-2020, 11:06 AM)Max Schmeling Wrote:
(05-18-2020, 04:55 PM)TheOldPresbyope Wrote: @Max Schmeling

What, no "try it and see" test  Tongue

The airplayactv parameter hasn't changed in 6.5.2. I think your code will work unmodified.

However, you ought to consider updating it to Python3. At some point the Debian maintainers will remove the Python 2 stack and I expect the Raspbian maintainers will too.

Regards,
Kent

@TheOldPresbyope 

Thanks. Smile  I just tried updating to P3 and installing python-mpd2. Everything seems to work, but I'll have to check, once I get it in the box with the buttons. I'll report back Smile

Right Confused . Guess I have to install the python-mdp2 with pip3 and not pip. Big Grin Now it is working Smile
Max Schmeling
Reply


Forum Jump: