数据库SQL文使用命令参数代替拼接SQL语句

来源:互联网 发布:录制广告语软件 编辑:程序博客网 时间:2024/05/20 02:55

做数据库的常识,不要拼接SQL语句,应使用命令参数更安全,不需要转义。

import sqlite//打开数据库连接var sqlConnection = sqlite("/testParameters.db")//创建表if( not sqlConnection.existsTable("film") ){      sqlConnection.exec( "create table film(title, length, year, starring);")  }  //可以用@表示命名参数var command = sqlConnection.prepare("insert into film values (@title,@length,@year, 'Jodie Foster');" )//绑定命名参数command.bind.parameterByNamesAt(    title = "I'm Jack"; //字符串不需要转义    length = 4;    year = time.now();).step();//释放命令对象command.finalize()io.open();for title, length, year, starring in sqlConnection.each("select * from film") {    io.print( title, length, year, starring  )} 
使用参数是最通用,也不会出错的.拼接的sql不但容易错,还会让程序带来
sql注入的风险,这在网页应用中最明显。

摘自:sqlite语句有特殊字符,怎么处理?

百度百科:参数化查询


原创粉丝点击