阅读背景:

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

来源:互联网 

做数据库的常识,不要拼接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  )
} import



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: