想要将本地excel文件名作为表名,表格中第一行作为字段,现在好像是sql语句不能使用变量?有帮助必采纳
def ct(request):
if request.method == 'GET':
return render(request, "create_table.html")
if request.method == 'POST':
f = request.FILES.get('file')
path = r"D:\pythonProject3\bphweb\app01\static\upload_excel" # 文件夹目录
files = os.listdir(path)
excel_name = files[0].split(".xlsx")[0]
print(excel_name)
wb = xlrd.open_workbook(filename=excel_name,file_contents=f.read())
table = wb.sheets()[0]
nrow_value = table.row_values(0)
print(nrow_value)
ziduan_1 = nrow_value[0]
ziduan_2 = nrow_value[1]
ziduan_3 = nrow_value[2]
from django.db import connection
cursor = connection.cursor()
sqll = "create table %s (%s int,%s int,%s int);"
cursor.execute(sqll,[excel_name,ziduan_1,ziduan_2,ziduan_3])
return HttpResponse("建立成功")