Accessing remote MySQL database with peewee
Asked Answered
P

1

27

I'm trying to connect to a MySQL database on Amazon's RDS using peewee and I can't get it to work. I'm new to databases so I'm probably doing something stupid, but this is what I'm trying:

import peewee as pw

myDB = pw.MySQLDatabase(host="mydb.crhauek3cxfw.us-west-2.rds.amazonaws.com",port=3306,user="user",passwd="password",db="mydb")


class MySQLModel(Model):
    """A base model that will use our MySQL database"""
    class Meta:
        database = myDB

class User(MySQLModel):
    username = CharField()

myDB.connect()

it hangs up on the second line, saying __init__() takes at least 2 arguments (1 given)

What am I missing? Why is it saying I'm only giving it one argument when I'm giving it five?

Thanks a lot, Alex

Putscher answered 8/5, 2013 at 18:58 Comment(0)
P
40

I changed it to be like this and it worked:

import peewee as pw

myDB = pw.MySQLDatabase("mydb", host="mydb.crhauek3cxfw.us-west-2.rds.amazonaws.com", port=3306, user="user", passwd="password")

class MySQLModel(pw.Model):
    """A base model that will use our MySQL database"""
    class Meta:
        database = myDB

class User(MySQLModel):
    username = pw.CharField()
    # etc, etc


# when you're ready to start querying, remember to connect
myDB.connect()

Thanks guys, Alex

Putscher answered 8/5, 2013 at 21:31 Comment(4)
As you noted, max_length=None is not correct. Should either be omitted or max_length=<int>Termor
You're right, although it worked for above, it broke the code when I tried to create tables using User.create_table(). I'll remove it from above.Putscher
Thank you! Could you please tell how to perform SELECT * FROM table after connection?Penza
How to specify table for inserting data?Mindful

© 2022 - 2024 — McMap. All rights reserved.