Chinaunix首页 | 论坛 | 博客
  • 博客访问: 870051
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: 系统运维

2012-05-06 00:17:08

-- loaddriver

require"luasql.mysql"

-- createenvironmentobject

env = assert (luasql.mysql())

-- connecttodatasource

con = assert (env:connect("database", "usr", "password", "192.168.xx.xxx", 3306))

-- resetourtable

res = con:execute"DROP TABLE people"               --建立新表people

res = assert (con:execute[[

         CREATETABLEpeople(              

                   namevarchar(50),

                   emailvarchar(50)

                   )

]])

-- addafewelements

list = {

         { name="Jose das Couves", email="jose@couves.com", },

         { name="Manoel Joaquim", email="manoel.joaquim@cafundo.com", },

         { name="Maria das Dores", email="maria@dores.com", },

         }

         fori, pinpairs (list) do                                            --加入数据到people表

                   res = assert (con:execute(string.format([[

                            INSERTINTOpeople                                         

                                     VALUES ('%s', '%s')]], p.name, p.email)

                                     ))

                                     end

                                     -- retrieveacursor

                                     cur = assert (con:execute"SELECT name, email from people")    --获取数据

                                     -- printallrows

                                     row = cur:fetch ({}, "a") -- therowswillbeindexedbyfieldnames    --显示出来

                                     while row do

                            print(string.format("Name: %s, E-mail: %s", row.name, row.email))

                                     row = cur:fetch (row, "a") -- reusingthetableofresults

                                     end

                                     -- closeeverything

cur:close()

con:close()

env:close()
阅读(1292) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~