2010-08-08 7 views

답변

4

: LuaSQL에 대한

require "luasql.mysql" 
env = assert (luasql.mysql()) 
con = assert (env:connect"my_db") 
for id, name, address in rows (con, "select * from contacts") do 
    print (string.format ("%s: %s", name, address)) 
end 
5

최소 워킹 예 - DBMS에에 루아에서 간단한 인터페이스를 제공합니다.

package.cpath = package.cpath .. ";/usr/lib/i386-linux-gnu/lua/5.1/?.so" 

luasql = require "luasql.mysql" 

env = assert (luasql.mysql()) 
con = assert (env:connect("dbname","user","password")) 
cur = assert (con:execute("SHOW TABLES")) 

row = cur:fetch ({}, "a") 
while row do 
    print(string.format("Name: %s", row.Tables_in_dbname)) 
    row = cur:fetch (row, "a") 
end 

luasql.mysql 모듈을 찾을 수없는 경우 사용되는 행 1입니다. 또한 환경 변수 LUA_CPATH가 사용될 수 있습니다.

+0

데이터베이스에 암호가 있다면 어떨까요? – 71GA

+1

@ 71GA at line 6.'env : connect'의 세번째 인자. –

3

mysql 데이터베이스가 원격 인 경우 호스트를 다른 선택적 매개 변수로 연결하여 연결할 수 있습니다. 포트뿐만 아니라 호스트를 따를 수 : 그것을 발견

con = assert (env:connect("dbname","user","password","host",port)) 
+0

암호로 보호 된 데이터베이스는 어떻습니까? – 71GA

관련 문제