函数在Iron Python(Python(x,y)Spyder2 IDE)中运行,但不在Python 2.7控制台

问题描述:

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

下面是代码:

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" 
+0

在尝试插入之前,您从“print first”获得了什么? – roganjosh

+0

Spyder保持运行之间的状态;这是一些指定的值仍然存在,并且您可能已删除的帮助程序功能仍然存在。你的函数是否仍然在新内核上运行(重启spyder)? –

+0

@roganjosh我打印的sql变量,工作正常。 –

我加在@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())