python判断sqlite数据库是否存在_如何判断一个值是否存在于sqlite3数据库中,python...

您只需在插入之前进行查询,然后执行^{}。如果fetchone返回了某些内容,那么您肯定知道数据库中已经有一个记录具有email或{}:def signup():

email = request.form['email']

username = request.form['user']

password = request.form['password']

# Create cursor object

cur = g.db.cursor()

# run a select query against the table to see if any record exists

# that has the email or username

cur.execute("""SELECT email

,username

FROM users

WHERE email=?

OR username=?""",

(email, username))

# Fetch one result from the query because it

# doesn't matter how many records are returned.

# If it returns just one result, then you know

# that a record already exists in the table.

# If no results are pulled from the query, then

# fetchone will return None.

result = cur.fetchone()

if result:

# Record already exists

# Do something that tells the user that email/user handle already exists

else:

cur.execute("INSERT INTO users VALUES (?, ?, ?)", (email, username, password))

g.db.commit()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值