sqlite PeeWee Relational Databases - Getting TypeError:应为str,而不是ModelBase

sigwle7e  于 2023-03-23  发布在  SQLite
关注(0)|答案(1)|浏览(147)

我正在使用SQLite和peewee,遇到了一个TypeError,我不知道如何修复。这是我的程序。

file = 'network.db'
if os.path.exists(file):
os.remove(file)

db = pw.SqliteDatabase(pw.Model)

class BaseModel(pw.Model):
    class Meta:
        database = db

class User(BaseModel):
    '''I define my User Class here'''

class Status(BaseModel):
    '''I define my Status Class here'''

def main():
    db.connect()
    db.execute_sql('PRAGMA foreign_keys = ON;')
    db.create_tables([
        User,
        Status])
 
   users = [
            (user1, test1, name1),
            (user2, test2, name3),
            (user3, test3, name3)]

    for account in users:
        try:
            with db.transaction():
                new_account = User.create(
                    user_id=account[0],
                    email=account[1],
                    user_name=account[2],
                    user_last_name=account[3])
                new_account.save()
    except Exception as e:

    status = [
            (status1, user1, text1),
            (status2, user2, text3),
            (status3, user3, text3)]

    for message in status:
        try:
            with db.transaction():
                new_status = Status.create(
                    status_id=message[0],
                    user_id=message[1],
                    status_text=message[2],
                )
            new_status.save()
        except Exception as e:

    db.close()

main()

当我运行它时,它会给我这个错误:

Traceback (most recent call last):
  File "C:\Documents\folder\network_model.py", line 105, in <module> 
func()
  File "C:\Documents\folder\assignment-03-parkertheoj\network_model.py", line 52, in func
db.connect()
  File "C:\Documents\folder\venv\lib\site-packages\peewee.py", line 3177, in connect
self._state.set_connection(self._connect())
  File "C:\Documents\folder\venv\lib\site-packages\peewee.py", line 3521, in _connect
conn = sqlite3.connect(self.database, timeout=self._timeout,
TypeError: expected str, bytes or os.PathLike object, not ModelBase

在我看来,有错误是来自peewee模块本身,但我不知道如何修复。任何帮助将不胜感激!谢谢!

xnifntxz

xnifntxz1#

你到底从哪弄来的db = pw.SqliteDatabase(pw.Model)
应该是

db = pw.SqliteDatabase('path/to/database-file.db')

相关问题