python_在sqlite中创建表并写入表头
import sqlite3def write_title_to_sqlite(tableName,titleList,dataTypeGroupsList,database_path):conn = sqlite3.connect(database_path)# 创建游标cursor = conn.cursor()#MEMO 长文本#create_table_bodycreate_table_body = "序号 INTEGER PRIMARY KEY AUTOINCREMENT,\n"for title in titleList:# 检查标题是否在aa的第一个子列表中if title in dataTypeGroupsList[0]:field_type = "TEXT(50)"# 如果不在第一个子列表中,并且在第二个子列表中,则其类型为"DATE"elif title in dataTypeGroupsList[1]:field_type = "DATE"# 使用字典中的默认类型,确保所有标题都能找到对应的类型else:field_type = "TEXT(50)"create_table_body += f"{title} {field_type},\n"# 移除最后一个逗号和换行符create_table_body = create_table_body.rstrip(",\n")create_table_sql = f"""CREATE TABLE IF NOT EXISTS {tableName} ({create_table_body});"""# 首先检查表是否存在,如果存在则删除drop_table_query =f"""DROP TABLE IF EXISTS {tableName};"""# 执行删除表的命令cursor.execute(drop_table_query)# 动态生成列名部分columns = ", ".join(titleList)# 动态生成问号占位符部分,问号数量与titleList长度相同placeholders = ", ".join(["?" for _ in titleList])# 执行创建表的命令cursor.execute(create_table_sql)# 提交事务conn.commit()# 关闭游标和连接cursor.close()conn.close()return placeholders