Search code examples
mysqlsqliteormpython-2.7peewee

connecting mysql with peewee and some issue accessing a table


I'm doing a web development for the first time in my life in linux and it will be later ported onto an embedded system once it works on linux machine. I have a table named Login in mydb database with password. I'm using peewee as ORM for connection. This is the script I'm running

from peewee import *
mysql_db = MySQLDatabase('mydb', user='root',passwd='rakesh')
class Login(Model):
    usr_name = CharField()
    passwd = TextField()

mysql_db.connect()
usr = Login(usr_name="me", passwd='Peewee is cool')
usr.save()
for user in usr.filter(usr_name="me"):
    print usr.title

I have created a simple table named Login in mydb database(mysql) and it seems to be okay when I query and see

now when I run this code I get an exception

Traceback (most recent call last):
  File "/home/rakesh/Ubuntu One/PDNFlywheels/PDB/access_ex.py", line 53, in <module>
    usr.save()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1949, in save
    new_pk = insert.execute()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1355, in execute
    result = self.database.execute(self)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1446, in execute
    return self.execute_sql(sql, params, commit)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1449, in execute_sql
    cursor = self.get_cursor()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1420, in get_cursor
    return self.get_conn().cursor()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1413, in get_conn
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1401, in connect
    self.__local.conn = self._connect(self.database, **self.connect_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1541, in _connect
    return sqlite3.connect(database, **kwargs)
OperationalError: unable to open database file

as you can see in the last line its trying to find the db using sqllite3 but i'm using mysql. I don't understand why this is happening. Most of the peewee cook books, examples and other explainations all use sqllite I never found any straight mysql hack.


Solution

  • You're missing the directive for which database to use. Please read the docs.

    http://peewee.readthedocs.org/en/latest/peewee/cookbook.html#using-with-mysql

    mysql_db = MySQLDatabase('mydb', user='root',passwd='rakesh')
    
    class Login(Model):
        usr_name = CharField()
        passwd = TextField()
    
        class Meta:
            database = mysql_db