在SQLITE数据库中,如果我需要表元的详细信息,我可以运行下面的命令
C:\sqlite>sqlite3.exe sqlite2.db
SQLite version 3.7.15 2012-12-12 13:36:53
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA table_info(forum_forum);
0|id|integer|1||1
1|category_id|integer|0||0
2|name|varchar(100)|1||0
3|description|varchar(200)|1||0
4|locked|bool|1||0
我想在 sqlalchemy
中做类似的事情。有人可以告诉我该怎么做吗?
解决方案
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
db_target = create_engine('sqlite:///C:\\Users\\asit\\workspace\\forum1\\src\\sqlite.db')
session = sessionmaker(db_target, autocommit = True)()
rs = session.execute("PRAGMA table_info(forum_forum)")
for row in rs:
print '%s %s %s %s %s' % (row['cid'], row['name'], row['type'], row['notnull'], row['pk'])
输出:-
0 id integer 1 1
1 category_id integer 0 0
2 name varchar(100) 1 0
3 description varchar(200) 1 0
4 locked bool 1 0
最佳答案
您可以使用 session.execute()
运行任意 SQL 语句,包括该 pragma 语句。
PRAGMA table_info(forum_forum)
语句返回一系列行:
>>> res = session.execute("PRAGMA table_info(forum_forum)")
>>> res.keys()
[u'cid', u'name', u'type', u'notnull', u'dflt_value', u'pk']
>>> for row in res:
... print row
...
(0, u'id', u'integer', 0, None, 1)
(1, u'category_id', u'integer', 0, None, 0)
(2, u'name', u'varchar(100)', 1, None, 0)
(3, u'description', u'varchar(200)', 1, None, 0)
(4, u'locked', u'bool', 1, None, 0)
关于python - sqlite3 中的列详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389793/