#!/usr/bin/env pypthon#_*_ coding: utf-8 _*_importsqlite3SCHEMA=""" CREATE TABLE person ( p_id int, p_name text ) """definit():data=[(1,'tony'),(1,'jack')]conn=sqlite3.connect(':memory:')c=conn.cursor()try:c.execute(SCHEMA)forpersonindata:c.execute('insert into person values(?, ?)',person)conn.commit()exceptsqlite3.Errorase:print'error!',e.args[0]returnconnif__name__=='__main__':conn=init()c=conn.cursor()#Do a query.c.execute('select * from person where p_name = ?','tony')person=c.fetchone()printperson
运行这段代码,抛出了个异常,如下提示:
1234
Traceback(mostrecentcalllast):File"sqlite3_test.py",line32,in<module>c.execute('select * from person where p_name = ?','tony')sqlite3.ProgrammingError:Incorrectnumberofbindingssupplied.Thecurrentstatementuses1,andthereare4supplied.
t=(’RHAT’,)c.execute(’SELECT*FROMstocksWHEREsymbol=?’,t)所以将字符参数放到元组中就可以了,修改如下:c.execute('select * from person where p_name = ?',('tony'))结果依旧是抛出了同样的异常。再仔细看下,漏了个',',于是加上:c.execute('select * from person where p_name = ?',('tony',))这次终于得到最终的结果了,其中的字符为unicode类型:(1,u'tony')
.../* execute() */if(!PyArg_ParseTuple(args,"O|O",&operation,&second_argument)){gotoerror;}if(!PyString_Check(operation)&&!PyUnicode_Check(operation)){PyErr_SetString(PyExc_ValueError,"operation parameter must be str or unicode");gotoerror;}parameters_list=PyList_New(0);if(!parameters_list){gotoerror;}if(second_argument==NULL){second_argument=PyTuple_New(0);if(!second_argument){gotoerror;}}else{Py_INCREF(second_argument);}if(PyList_Append(parameters_list,second_argument)!=0){Py_DECREF(second_argument);gotoerror;}Py_DECREF(second_argument);parameters_iter=PyObject_GetIter(parameters_list);if(!parameters_iter){gotoerror;}...