Issue: Is there a way to connect to a database not in the same folder?
Author: jacobbieker
State: closed
Created: 2015-07-14 02:28:33

I am tyring to connect to a local SQLite database to store output from multiple scripts in one place. The problem is that when I try to connect to the database from a different folder, it just creates a peewee.db in the current directory. Does Database URLs allow for me to connect to a local database correctly? From the docs, it sounded like the connect() call cimply created a new datanase for those URLs. Any help would be appreciated, I quite possibly just didn't see it in the docs. Thanks!


Are all of your models explicitly referencing your db?

``` python
db = SqliteDatabase('mydb')
class SomeModel(Model):
    data = CharField()
    class Meta:
        database = db # do you have this?
```


Yes, they are. I followed the examples and have a BaseModel that references the database, and that all the other models extend. For when I am trying to connect form a different folder, I use the absolute path to the database for the <code>db = SqliteDatabase('absolute-path-to-database')</code> 

If it helps, the script that I am using to create and connect to the database is located here: 
https://github.com/jacobbieker/Insights/blob/dev/databaseSetup.py

I import that file into the other scripts to access the database, unless there is a better way to do it?


Can you share your code?


Yes, here is where I connect to a database that is run whenever databaseSetup is imported, the whole file is here: https://github.com/jacobbieker/Insights/blob/dev/databaseSetup.py:

```
if __name__ != "__main__":
    with open(os.path.join("..", "constants.yaml"), 'r') as ymlfile:
        config = yaml.load(ymlfile)
else:
    with open("constants.yaml", 'r') as ymlfile:
        config = yaml.load(ymlfile)
class BaseModel(peewee.Model):
    class Meta:
        database = SqliteDatabase(config.get('databaseLoc')).connect()

    class Locations(BaseModel):
        date = DateTimeField(null=True)
        time = TimeField(null=True)
        longitude = DoubleField(null=True)
        latitude = DoubleField(null=True)
        continent = CharField(null=True)
        country = CharField(null=True)
        state = CharField(null=True)
        zip = IntegerField(null=True)
        city = CharField(null=True)
        street = TextField(null=True)
        name = TextField(null=True)
```

Where 'databaseLoc' is the absolute path to the desired database

And here is the relevant code in a script that imports 

Locations2SQLite.py, located here: https://github.com/jacobbieker/Insights/blob/dev/GoogleTakeoutScripts/Location2SQLite.py

```
from databaseSetup import Locations

 Locations.insert(date=converted_time_stamp,time=time_stamp, longitude=longitude, latitude=latitude,
                         continent=continent, country=country, state=state, zip=zipcode, city=city, street=street,
                         name=building).execute()
```

When Locations2SQLite.py exits, it creates a peewee.db in its directory, and does not connect to the database created in databaseSetup.py 


The problem is you're calling `database.connect()` in the `Meta` inner class -- not sure why you're doing that there, since the return value of `connect` would not be a database instance.

Change it to the following and you should be fixed:

``` python
class BaseModel(peewee.Model):
    class Meta:
        database = SqliteDatabase(config.get('databaseLoc'))
```


That worked! Thanks for the help.