sqlalchemy的例子不起作用

时间:2015-12-19 19:23:10

标签: python python-3.x sqlalchemy

这不起作用。从here

获得
#include <map>

int main()
{
    std::map< char, unorderedLinkedList<string> > listMap;

    nodeType<string> *item = new nodeType<string>();
    item->info = "trying";
    item->link = NULL;
    item->wCount = 0;

    char first; 
    first = item->info[0];

    if (listMap[first].search(item->info))
    { 
        item->wCount++;
    }
    else
    { 
        listMap[first].insertFirst(item->info);
    }

    // ...
}

如果我尝试# -*- coding: utf-8 -*- """ Created on Sat Dec 19 13:19:13 2015 @author: idf """ import sqlite3 import sqlalchemy import sys from sqlalchemy import * print(sqlite3.version) print(sqlite3.sqlite_version) print(sqlalchemy.__version__) print(sys.version) # This prints: # 2.6.0 # 3.8.4.1 # 1.0.9 # 3.5.1 |Anaconda 2.4.0 (64-bit)| (default, Dec 7 2015, 11:16:01) #if I use this # db = create_engine('sqlite:///joindemo.db') # I get the error at end of post db = create_engine('sqlite', opts={'filename': 'joindemo.db'}) db.echo = True users = Table('users', db, Column('user_id', Integer, primary_key=True), Column('name', String(40)), Column('age', Integer), ) users.create() emails = Table('emails', db, Column('email_id', Integer, primary_key=True), Column('address', String), Column('user_id', Integer, ForeignKey('users.user_id')), ) emails.create() i = users.insert() i.execute( {'name': 'Mary', 'age': 30}, {'name': 'John', 'age': 42}, {'name': 'Susan', 'age': 57}, {'name': 'Carl', 'age': 33} ) i = emails.insert() i.execute( # There's a better way to do this, but we haven't gotten there yet {'address': 'mary@example.com', 'user_id': 1}, {'address': 'john@nowhere.net', 'user_id': 2}, {'address': 'john@example.org', 'user_id': 2}, {'address': 'carl@nospam.net', 'user_id': 4}, ) def run(stmt): rs = stmt.execute() for row in rs: print(row) # This will return more results than you are probably expecting. s = select([users, emails]) run(s) # The reason is because you specified no WHERE clause, so a full join was # performed, which returns every possible combination of records from # tables A and B. With an appropriate WHERE clause, you'll get the # restricted record set you really wanted. #s = select([users, emails], emails.c.user_id == users.c.user_id) #run(s) # If you're interested in only a few columns, then specify them explicitly #s = select([users.c.name, emails.c.address], # emails.c.user_id == users.c.user_id) #run(s) # There are also "smart" join objects that can figure out the correct join # conditions based on the tables' foreign keys #s = join(users, emails).select() #run(s) # If you want all the users, whether or not they have an email address, # then you want an "outer" join. #s = outerjoin(users, emails).select() #run(s) # Order of outer joins is important! Default is a "left outer join", which # means "all records from the left-hand table, plus their corresponding # values from the right-hand table, if any". Notice how this time, Susan's # name will *not* appear in the results. #s = outerjoin(emails, users).select() #run(s) >>> runfile('/home/idf/Documents/Projects/python3/testsqlalchemy.py', wdir='/home/idf/Documents/Projects/python3') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/idf/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "/home/idf/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace) File "/home/idf/Documents/Projects/python3/testsqlalchemy.py", line 10, in <module> db = create_engine('sqlite', opts={'filename': 'joindemo.db'}) File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/__init__.py", line 386, in create_engine return strategy.create(*args, **kwargs) File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py", line 49, in create u = url.make_url(name_or_url) File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/url.py", line 186, in make_url return _parse_rfc1738_args(name_or_url) File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/url.py", line 235, in _parse_rfc1738_args "Could not parse rfc1738 URL from string '%s'" % name) sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'sqlite' >>>

,这是另一个错误
db = create_engine('sqlite:///joindemo.db')

1 个答案:

答案 0 :(得分:3)

本教程不是最新的。正确的engine creation应为:

db = create_engine('sqlite:///joindemo.db')

您需要初始化MetaData object now

metadata = MetaData(db)

并在定义表时使用它:

users = Table('users', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('name', String(40)),
    Column('age', Integer),
)
users.create()

emails = Table('emails', metadata,
    Column('email_id', Integer, primary_key=True),
    Column('address', String),
    Column('user_id', Integer, ForeignKey('users.user_id')),
)
emails.create()

作为旁注,我会寻找一个不同的教程,这个教程并没有真正反映今天的SQLAlchemy