How to use prepared statements in lua-dbi?

前提是你 提交于 2019-12-11 03:53:44

问题


I want to use prepared statements in my lua scripts. As mentioned in my previous post, people recommend using lua-dbi. Unfortunately there is little documentation available. I just need a basic script that connects to the database with credentials, and use prepared statements (prefered with a bind function to names in the query). Anyone experienced with this?


回答1:


You can find it on the project's wiki pages:

Establishing connection: https://code.google.com/p/luadbi/wiki/DBDDriverConnection

require('DBI')

-- Create a connection
local dbh = assert(DBI.Connect('Driver', db, username, password, host, port))

-- set the autocommit flag
-- this is turned off by default
dbh:autocommit(true)

-- check status of the connection
local alive = dbh:ping()

-- prepare a connection
local sth = assert(dbh:prepare(sql_string))

-- commit the transaction
dbh:commit()

-- finish up
local ok = dbh:close()

where, you'd update the part dbh:prepare as per your needs.



来源:https://stackoverflow.com/questions/32677927/how-to-use-prepared-statements-in-lua-dbi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!