Django中使用views.py自动递增

发布于 2025-01-11 18:59:16 字数 1180 浏览 0 评论 0原文

if request.method == 'POST':
    m = sql.connect(host="localhost",user="root",password="admin@123", database= "ticketsys")
    cursor = m.cursor()

    ticketid = request.POST['ticketid']
    email = request.POST['email']
    username = request.POST.get('username')
    checkbox = request.POST['checkbox']
    adult = request.POST['adult']
    fromstation = request.POST['fromstation']
    tostation = request.POST['tostation']
    rate = request.POST["rate"]
    date = request.POST.get('date')
    usermessage = request.POST['usermessage']


    c = "insert into ticketsys.ticketgen values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(ticketid,email,username,checkbox,adult,fromstation,tostation,rate ,date, usermessage)
    cursor.execute(c)
    m.commit()

    return render(request, 'account/ticketconf.html')


else:
    return render(request, 'account/ticket.html')

在这里,票证 ID 是自动递增的,但它显示错误。

1366 (HY000): Incorrect integer value: '' for column 'ticketid' at row 1

解决这个问题的最好方法是什么。这样,无需创建模型,我就可以将数据发布到 mysql 中。

SQL-图像

if request.method == 'POST':
    m = sql.connect(host="localhost",user="root",password="admin@123", database= "ticketsys")
    cursor = m.cursor()

    ticketid = request.POST['ticketid']
    email = request.POST['email']
    username = request.POST.get('username')
    checkbox = request.POST['checkbox']
    adult = request.POST['adult']
    fromstation = request.POST['fromstation']
    tostation = request.POST['tostation']
    rate = request.POST["rate"]
    date = request.POST.get('date')
    usermessage = request.POST['usermessage']


    c = "insert into ticketsys.ticketgen values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(ticketid,email,username,checkbox,adult,fromstation,tostation,rate ,date, usermessage)
    cursor.execute(c)
    m.commit()

    return render(request, 'account/ticketconf.html')


else:
    return render(request, 'account/ticket.html')

Here, ticket id is auto-increment but its showing me error.

1366 (HY000): Incorrect integer value: '' for column 'ticketid' at row 1

What is best way to solve this problem. so that without creating model I can be able to post my data in mysql.

SQL- Image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文