0

When I try to return the value at a specific position from my database and store the value to a text file I get the following error:

 Argument must be a string or a number, not 'ResultProxy'.

int(expire) and str(expire) won't convert a 'ResultProxy'.

 def expire():
      today = datetime.date.today()
      day = today.strftime('%d %b %y')
      conn = engine.connect()
      sql = text('select account.expire from account where account.user_name = "%s"'%('Bob'))
      expire = conn.execute(sql)
      filename = 'mysite/expiry.txt'
      read = open(filename, 'r')

      target = open(filename, 'w')
      target.truncate()
      target.write(str(expire))
      target.write("\n")
      target.close()

      read = open(filename, 'r')
      daysleft = read

 return render_template('expire.html', daysleft=daysleft)

how do I convert the ResultProxy into a string?

Ricky92d
  • 181
  • 1
  • 2
  • 8

2 Answers2

1

Executing a query always returns a list of rows, a ResultProxy in SQLAlchemy's case. You are trying to write this result object to the file, rather than the actual result. Since it looks like you only expect one result, just make sure there's one result to write.

results = conn.execute(sql)

if results:
    expire = results[0]
    # write it to the file

Or if you expect multiple results, loop over them.

results = conn.execute(sql)

for expire in results:
    # write it to the file
davidism
  • 121,510
  • 29
  • 395
  • 339
0

Here is my suggestion on how you can do it in Flask-SQLAlchemy.

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)

Create the model that SQLAlchemy uses. I am assuming your table has 3 fields, a primary key, a user_name, and the expires field, which I assume is an integer from your use of this field as daysleft.

class Account(db.Model):
    __tablename__ = 'account' # Sets the actual name of the table in the db
    user_id = db.Column(db.String, primary_key=True)
    user_name = db.Column(db.String)
    expire = db.Column(db.Integer) 

Here is your function that will use the model.

def expire():
    today = datetime.date.today()
    day = today.strftime('%d %b %y')
    username = 'Bob'

Query the Account model (which is connected to the db via SQLAlchemy), filtering on the user_name field, and asking only for the first record returned, if any.

    account = db.session(Account).filter_by(user_name=username).first()
    filename = 'mysite/expiry.txt'
    read = open(filename, 'r')

    target = open(filename, 'w')
    target.truncate()

If the previous query didn't return any rows, account will be None. Otherwise, it will be a dict with each returned field as a property. You can access the value of the expire field using account.expire.

    if account != None:
        target.write(str(account.expire))
    target.write("\n")
    target.close()

    read = open(filename, 'r')
    daysleft = read

    return render_template('expire.html', daysleft=daysleft)
Kelly Keller-Heikkila
  • 2,544
  • 6
  • 23
  • 35