【问题标题】:How to pass QLineEdit text to MS Access DB?如何将 QLineEdit 文本传递给 MS Access DB?
【发布时间】:2014-08-23 20:41:10
【问题描述】:

我正在使用 INSERT 查询,如果我在 VALUES 中传递一个字符串,它可以正常工作,如下所示:

oConn.Execute("Insert into Student_Info(Student_Name) values ('Robin')")

但是如果我以这种方式传递 QLineEdit.text() 会出现错误:

oConn.Execute("Insert into Student_Info(Student_Name) values ('"& (self.StudentName.text()) &"')")

错误:

oConn.Execute("Insert into Student_Info(Student_Name) values ('"& (self.StudentName.text()) &"')")
TypeError: unsupported operand type(s) for &: 'str' and 'QString'

请指教,我不知道出了什么问题。

【问题讨论】:

    标签: python python-2.7 ms-access qt4 pyqt4


    【解决方案1】:

    你可以使用 python 传递字符串简单的方法,像这样;

    command = '''Insert into Student_Info(Student_Name) values ('%s')''' % str(self.StudentName.text())
    oConn.Execute(command)
    

    或者你可以使用字符串连接;

    command = "Insert into Student_Info(Student_Name) values ('" + str(self.StudentName.text()) + "')"
    oConn.Execute(command)
    

    【讨论】:

    • 谢谢,但你能建议我的代码有什么问题吗?
    • 与号 (&) 运算符在位 OR 运算符中使用,而不是使用字符串 concat 运算符。如果您想使用字符串连接,我建议使用加号(+)运算符。
    • 我用过这个,效果很好:oConn.Execute("Insert into Student_Info(Student_Name) values ('"+ (self.StudentName.text() +"')
    • 是的,应该是这样。但是在您的问题中,您使用与号 (&) 运算符。它有错误。
    • 抱歉,与号 (&) 运算符用于位 AND 运算符而不是 OR 运算符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    相关资源
    最近更新 更多