<p>我相信你可以用正则表达式或者<a href="https://pypi.python.org/pypi/sqlparse" rel="nofollow noreferrer">^{<cd1>}</a>来解决这个问题,但是这里有一个“有趣”的方法来解决这个问题仅仅是为了教育目的-在内存</em>数据库中使用<a href="https://docs.python.org/2/library/sqlite3.html" rel="nofollow noreferrer">^{<cd2>}</a><em>-实际创建表并从<a href="https://stackoverflow.com/a/10746045/771848">^{<cd4>} internal table</a>中获取<code>table_name</code>,从<a href="https://www.sqlite.org/pragma.html#pragma_table_info" rel="nofollow noreferrer">^{<cd5>}</a>中获取主键列:</p>
<pre><code>import sqlite3
query = """
CREATE TABLE 'dhcpr_dhcprelayinterface' (
'vrId' integer default 0,
'ifName' string ,
PRIMARY KEY(ifName,vrId),
FOREIGN KEY (vrId) REFERENCES 'vr_vr'(vrId) ON DELETE CASCADE ON UPDATE CASCADE);
"""
db = sqlite3.connect(":memory:")
cursor = db.cursor()
cursor.execute(query)
db.commit()
# get table name
cursor.execute("select name from sqlite_master where type = 'table'")
table_name = cursor.fetchone()[0]
print(table_name)
# get primary key columns
cursor.execute("PRAGMA table_info(%s);" % table_name)
pk_columns = [row[1] for row in cursor.fetchall()[::-1]]
print(pk_columns)
</code></pre>
<p>印刷品:</p>
<pre><code>dhcpr_dhcprelayinterface
['ifName', 'vrId']
</code></pre>