0

I am trying to update an sqlalchemy database. It throws an error based off the string I try to implement into the query. I dont want to hardcode the link, but rather use the variable as I am looping through my database.

print (u)
(u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n',)


currentlink = (str(u)[:-3][1:])
print currentlink
u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n'

This fails..

x = Item.query.filter_by(link=currentlink).first()
            print x
            try:
                print x.id
                x.title = 'test'
            except Exception as e:
                print(str(e))

            db.session.commit()

prints :'NoneType' object has no attribute 'id'

This works..

x =  Item.query.filter_by(link=u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n').first()

            print x
            try:
                print x.id
                x.title = 'test'
            except Exception as e:
                print(str(e))

            db.session.commit()

prints: http://32rfckwuorlf4dlv.ca/linklist/1-general\n'> prints: 90

2
  • Well yea there's a row in your database with link = 'http://32rfckwuorlf4dlv.ca/client-check\n' but no row with link = 'http://32rfckwuorlf4dlv.ca/linklist/1-general\n'. Commented May 24, 2016 at 1:39
  • Used wrong link example..i believe it has to do with the way \n is represented. Commented May 24, 2016 at 1:43

2 Answers 2

1

Okay the reason is that currentlink is actually "u'http://32rfckwuorlf4dlv.ca/linklist/1-general\\n", not u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n'.

Assuming u is a tuple, you should do

currentlink = u[0]

instead.

If you want to make your example work (not recommended by the way, it's only for edification purposes):

currentlink = str(u)[:-3][3:].replace("\\n", "\n")

If u is a str instead, you need to figure out why it's a str instead of a tuple.

Sign up to request clarification or add additional context in comments.

Comments

1

two things

  1. seems the variable u in your description is a tuple, so you can just use currentlink = u[0] to get the url link.

  2. The currentlink u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n' and http://32rfckwuorlf4dlv.ca/client-check\n in hardcode are different. And I think this is the reason that you get a Nonetype from database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.