社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

我们可以用to python脚本创建注释吗?

Neophyte • 5 年前 • 1560 次点击  

我想在python中使用sqlite3创建一个表。我有一套像

Set = set(['CY110', 'PH100', 'CY100', 'CS100', 'BE110'])

正常创建表条件的锅炉板:

sql_create_table = """ CREATE TABLE IF NOT EXISTS marks (id integer PRIMARY KEY,name text NOT NULL, );"""

我遇到的问题是,我需要包括这样的每个元素:

sql_create_table = """ CREATE TABLE IF NOT EXISTS marks (id integer PRIMARY KEY,name text NOT NULL,CY100 text,PH100 text,CS100 text,BE100 text);"""

我该怎么做?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38153
 
1560 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Dodge
Reply   •   1 楼
Dodge    6 年前

如果您只想格式化该集合中的字符串,可以使用以下内容。也要考虑除 Set 姓名:

mySet = set(['CY110', 'PH100', 'CY100', 'CS100', 'BE110'])
mySet = list(mySet)  

sql_create_table = """ CREATE TABLE IF NOT EXISTS marks (id integer PRIMARY KEY,name text NOT NULL,{0} text,{1} text,{2} text,{3} text);""".format(mySet[0],mySet[1],mySet[2],mySet[3])

print(sql_create_table)

如果不转换为列表,则无法直接为集合编制索引以格式化字符串,因为集合的顺序没有如本文所述。 link

blhsing
Reply   •   2 楼
blhsing    6 年前

可以使用生成器表达式并将输出与 ',' 要生成所需的查询字符串,请执行以下操作:

sql_create_table = 'CREATE TABLE IF NOT EXISTS marks (id integer PRIMARY KEY,name text NOT NULL,' + ','.join('%s text' % c for c in Set)

使用您的示例输入, sql_create_table 会变成:

CREATE TABLE IF NOT EXISTS marks (id integer PRIMARY KEY,name text NOT NULL,BE110 text,CS100 text,CY100 text,PH100 text,CY110 text