Python操作SQLite 模块sqlite3

API地址,英文的。

'''
   这是一个连接数据库并更新表的一个范例
   虽然这是正确的但是这样对命令处理容易受到注入攻击,所以不安全。        
'''
import sqlite3
con = sqlite3.connect(dbfile)#连接到数据库
cu = con.cursor()#获取游标
strSqliteCmd = ("update list_dllstore set unitproperty01='%s' where  unitproperty03='%s'")  # 命令
cu.execute(strSqliteCmd % ("a", "b"))#输入命令
con.commit()#提交命令
cu.close()
con.close()

接下来示范一个正确的对命令以及参数的处理方式,这里就不做连接数据库之类的操作了

这一种方法是将值的位置以英文'?'作为占位符,然后在输入命令时将这些值以参数的形式输入而不是格式化字符。

strSqliteCmd = ("delete from list_paramterConfig where Dllname=? and vname=?;")
cu.execute(strSqliteCmd, (strategyName, dictTempPastParameters['vname']))
con.commit()

 

原文地址:https://www.cnblogs.com/ScarecrowMark/p/11290431.html