Python SQLAlchemy语法错误

时间:2011-09-05 02:54:34

标签: python sqlite sqlalchemy

运行以下程序时,我遇到了令人抓狂的语法错误。这是错误:

  File "Create_the_database.py", line 64

  ^
SyntaxError: invalid syntax

这是代码(很长,但重复性很容易撇去):

import sqlalchemy
from sqlalchemy import *

engine = create_engine('sqlite:///Users/benscholz/Dropbox/OmniCloud/omnicloud/dev.db')
meta = MetaData()
createUsersTable()
createAccountsTable()
createPasswordsTable()
meta.bind = engine
meta.create_all(engine)

def createUsersTable():
    print("creating User Table")
    users = Table('users', metadata,
        Column('id', Integer, primary_key = True),
        Column('username', String, nullable = False),
        Column('email_address', String, nullable = False),
        Column('first_name', String),
        Column('full_name', String),
        Column('join_date', DateTime))

def createAccountsTable():
    print("creating Accounts Table")
    accounts = Table('accounts', metadata,
        Column('user_id', ForeignKey('users.id')),
        Column('facebook_username', String),
        Column('email_username_1', String),
        Column('email_username_2', String),
        Column('email_username_3', String),
        Column('email_username_4', String),
        Column('email_username_5', String),
        Column('twitter_username_1', String),
        Column('twitter_username_2', String),
        Column('twitter_username_3', String),
        Column('klout_username', String),
        Column('dropbox_username', String),
        Column('box_username', String),
        Column('flickr_username_1', String),
        Column('flickr_username_2', String),
        Column('instagram_username', String),
        Column('disqus_username', String),
        Column('stack_overflow_username', String))

def createPasswordsTable():
    print("creating Passwords Table")
    passwords = Table('passwords', metadata,
        Column('user_id', ForeignKey('users.id')),
        Column('facebook_pass', String),
        Column('email_pass_1', String),
        Column('email_pass_2', String),
        Column('email_pass_3', String),
        Column('email_pass_4', String),
        Column('email_pass_5', String),
        Column('twitter_pass_1', String),
        Column('twitter_pass_2', String),
        Column('twitter_pass_3', String),
        Column('klout_pass', String),
        Column('dropbox_pass', String),
        Column('box_pass', String),
        Column('flickr_pass_1', String),
        Column('flickr_pass_2', String),
        Column('instagram_pass', String),
        Column('disqus_pass', String),
        Column('stack_overflow_pass', String))

这个程序是64行,所以它似乎到了最后并期待......某事?

1 个答案:

答案 0 :(得分:1)

在修好两件事后,它对我来说很好。

  1. 变量元应该是元数据。 Table()的第二个参数引用了全局变量。
  2. 从createUsersTable()到metadata.create_all(engine)的行应该向下移动到底部。在定义函数之前无法调用函数。
  3. 至于语法错误,我猜文件在最后一行有一些无效字符,无法在屏幕上打印。我的建议是删除最后一行,然后重新输入。