How to query MySQL database via python using peewee / mysqldb?

I am creating an iOS client for App.net and I am trying to set up a push notification server. Currently, my application can add the App.net user account identifier (string of numbers) and the APNS device token to the MySQL database on my server. It can also delete this data. I adapted the code from these two guides:

In addition, I adapted this awesome python script to listen to the App Stream App App API.

My python is terrible, as is my knowledge of MySQL. What I'm trying to do is access the APNS device token for the accounts I need to notify. My database table has two fields / columns for each entry, one for user_id and one for device_token. I am not sure of the terminology, please let me know if I can clarify this.

I'm trying to use peewee to read from a database, but I'm in my head. This is a test script with a placeholder user_id:

import logging
from pprint import pprint

import peewee
from peewee import *

db = peewee.MySQLDatabase("...", host="localhost", user="...", passwd="...")

class MySQLModel(peewee.Model):
    class Meta:
        database = db

class Active_Users(MySQLModel):
    user_id = peewee.CharField(primary_key=True)
    device_token = peewee.CharField()

db.connect()

# This is the placeholder user_id
userID = '1234' 

token = Active_Users.select().where(Active_Users.user_id == userID)
pprint(token)

Then issued:

<class '__main__.User'> SELECT t1.`id`, t1.`user_id`, t1.`device_token` FROM `user` AS t1 WHERE (t1.`user_id` = %s) [u'1234']

If the code doesn’t make it clear, I try to query the database for line c user_idin ' 1234', and I want to save device_tokenthe same line (again, probably the wrong terminology) into a variable that I can use when I send a push notification to the script later.

device_token? , peewee python-mysqldb? , ?

+4
1

User.select().where(User.user_id == userID) User, token, device_token.

:

matching_users = Active_Users.select().where(Active_Users.user_id == userID) # returns an array of matching users even if there just one
if matching_users is not None:
    token = matching_users[0].device_token
+4

All Articles