【问题标题】:Function runs in Iron Python (Python(x,y) Spyder2 IDE) but not in Python 2.7 Console函数在 Iron Python (Python(x,y) Spyder2 IDE) 中运行,但不在 Python 2.7 控制台中运行
【发布时间】:2017-09-16 14:24:36
【问题描述】:

我在 python(x,y) 包中使用 Spyder2 IDE。以下函数在 Iron Python 中运行良好,但在控制台中运行时出现错误。我需要它在控制台中运行,以便我可以使用 Pyinstaller。我得到的错误是:"Error binding parameter 0. Probably unsupported type." 显示错误的行是“cur.execute”行。我也在使用 Sqlite3 并从 PYQT4 lineEdit 字段中获取文本数据。

代码如下:

def update_clients(self):
    #Get client id from list
    cid = None
    try:
        cid = self.client_list_id()
    except:            
        QtGui.QMessageBox.warning(self, 'Warning', 'You must first select a client before you update')


    if cid:
        #Get update items
        first = self.lineEdit_c_first.text()        
        last = self.lineEdit_c_last.text()
        add1 = self.lineEdit_c_address1.text()
        add2 = self.lineEdit_c_address2.text()
        city = self.lineEdit_c_city.text()
        state = self.lineEdit_c_state.text()
        zipp = self.lineEdit_c_zip.text()        
        phone = self.lineEdit_c_phone.text()
        cell = self.lineEdit_c_phone_cell.text()
        off = self.lineEdit_c_phone_office.text()
        email = self.lineEdit_c_email.text()  
        notes = self.textEdit_c_notes.toPlainText() 
        #Update database
        conn = sqlite3.connect('gibbs.db')
        cur = conn.cursor()
        sql = ("""
        UPDATE clients
        SET 
        firstname = ?, 
        lastname = ?,
        address1 = ?,
        address2 = ?,
        city = ?,
        state = ?,
        zip = ?,
        phone = ?,
        officephone = ?,
        cell = ?,
        email = ?,
        notes = ?        
        WHERE rowid = ?
        """)
        cur.execute(sql, (first, last, add1, add2, city, state, zipp, phone, off, cell, email, notes, cid,))
        conn.commit()
        conn.close()

        QtGui.QMessageBox.information(self, 'Success', 'Database successfully updated')

此外,由于问题与数据类型有关,以下是我用来创建数据库表的代码:

import sqlite3

def create_connection():

    try:
        conn = sqlite3.connect('gibbs.db')
        return conn
    except:
        pass 
    return None

def create_clients():
    try:
        conn = create_connection()
        print conn
        c = conn.cursor()
        c.execute("""
        CREATE TABLE clients (
        id INTEGER PRIMARY KEY,
        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
        firstname TEXT,
        lastname TEXT,
        address1 TEXT,
        address2 TEXT,
        city TEXT,
        state TEXT,
        zip TEXT,
        phone TEXT,
        officephone TEXT,
        cell TEXT,
        email TEXT,
        notes TEXT    
        )     
        """)
        conn.close()
    except:
        print "table already exists"

【问题讨论】:

  • 在尝试插入之前你从print first 得到了什么?
  • Spyder 在运行之间保持状态;也就是说,一些分配的值仍然存在,并且您可能已删除的辅助函数仍然存在。您的函数是否仍在新内核上运行(重新启动 spyder)?
  • @roganjosh 我打印了 sql 变量,效果很好。
  • @roganjosh 非常感谢您挠头。此外,您可能正在做某事。 'Iron Python 输出中的打印类型(第一个):,在 Python2.7 控制台中为:
  • @roganjosh 我认为这可能有效。当我执行 str(first) 时,两个版本中的类型输出都是 并且 IP 版本仍然有效。现在我在控制台上的绑定参数“1”而不是“0”上出现错误。我认为它已经解决了。过几句我会告诉你的。谢谢!

标签: python python-2.7 sqlite pyqt4 ironpython


【解决方案1】:

按照@roganjosh 的建议,我在查询之前添加了print type(first) 行。运行程序时,两个控制台的结果不同。 Iron Python 控制台输出:<type 'unicode'>,在 Python 2.7 控制台中为:<class 'PyQt4.QtCore.QString'>

从 PYQT4 lineEdits 获取文本时,我通过添加 str() 函数解决了问题。例如:

first = str(self.lineEdit_c_first.text()) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多