【问题标题】:MysqlDb and Pyqt4 comparing variablesMysqlDb 和 Pyqt4 比较变量
【发布时间】:2014-10-09 17:14:52
【问题描述】:

我在 mysql 中有一个数据库,我在 QT4 中的一个应用程序中工作,我正在使用 Mysqldb 连接器,我已经成功连接到数据库,但是当我查询表时 t_usuarios 我不知道如何评价它。你能帮助我吗?提前致谢。

我的部分代码:

def chequeouser(self):
    passwdcheck = str(txt_password.text())
    usuariover =  str(txt_usuario.text())
   # datosLogin = "SELECT * FROM t_usuarios WHERE id_usuario = 'usuario' AND pasword = 'password'
    cursor = dbase.cursor()
    cursor.execute("SELECT id_usuario, password FROM t_usuarios WHERE id_usuario = %s AND password = %s", (usuariover, passwdcheck))
    checar = cursor.fetchone()

感谢回答我的用户...我做到了,但做了一些修改如下..

 passwdcheck = str(txt_password.text())
    usuariocheck =  str(txt_usuario.text())
   # datosLogin = "SELECT * FROM t_usuarios WHERE id_usuario = 'usuario' AND pasword = 'password'
    cursor = dbase.cursor()
    cursor.execute("SELECT id_usuario, password FROM t_usuarios WHERE id_usuario = %s AND password = %s", (usuariocheck, passwdcheck))
    row = cursor.fetchone()
    if row  == None:
        print "no data:"+ usuariocheck
        return 
    if row[1] == passwdcheck:
        print "user and password combination is correct" +usuariocheck
    else :
        print "your input is incorrect" 

我正在考虑这样做,但我不确定这是否正确。

【问题讨论】:

  • 我也用过 ... 这个 res = str(cursor.rowcount) 但也返回 0 ,
  • 在 PHP 中 ... 会使用 this.... [code]if(!$result || mysql_num_rows($result) HandleError("登录时出错. ". "用户名或密码不匹配");返回假; } 返回真; [/code] 但在 python 中我不知道...

标签: python mysql pyqt mysql-python


【解决方案1】:

fetchone() 返回一行数据。第一项是 row[0],第二项是 row[1],以此类推

例子:

def chequeouser(self):
    passwdcheck = str(txt_password.text())
    usuariover =  str(txt_usuario.text())
   # datosLogin = "SELECT * FROM t_usuarios WHERE id_usuario = 'usuario' AND pasword = 'password'
    cursor = dbase.cursor()
    cursor.execute("SELECT id_usuario, password FROM t_usuarios WHERE id_usuario = %s AND password = %s", (usuariover, passwdcheck))
    row = cursor.fetchone()
    if not row:
       print "no rows!"
       return
    if passwdcheck == row[1]:
       print 'MATCH for user', row[0]
    else:
       print 'uhoh'

【讨论】:

  • row = cursor.fetchone() if passwdcheck == row[1]: print 'MATCH for user', row[0] else: print 'uhoh' ...#### throws this错误.... if passwdcheck == row[1]: TypeError: 'NoneType' object has no attribute 'getitem'
  • 如果我将“cursor.fetchone()”更改为 cursor.fetchall()·····>> if passwdcheck == row[1]: IndexError: tuple index out of range
  • @mitsuzeroblackout 可能SELECT 不匹配任何行,因此fetchone() 返回无。我已经更新了代码。
  • 只是我做了一些更改,谢谢@shavenwarthog ...row = cursor.fetchone() if row == None: print "None in usuarios"+ usuariocheck return if row[1] == passwdcheck : print "usuario existse :" +usuariocheck else : print "错误:需要检查你的输入"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
相关资源
最近更新 更多