濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 簡(jiǎn)單的Lua 連接操作mysql數(shù)據(jù)庫(kù)的方法

簡(jiǎn)單的Lua 連接操作mysql數(shù)據(jù)庫(kù)的方法

熱門(mén)標(biāo)簽:陜西便宜電銷(xiāo)機(jī)器人軟件 海口智能語(yǔ)音電銷(xiāo)機(jī)器人好用嗎 杞縣地圖標(biāo)注app 昆明電話外呼系統(tǒng)好么 免費(fèi)門(mén)店地圖標(biāo)注注冊(cè)入駐 外呼系統(tǒng)一天耗費(fèi)多少流量 電話機(jī)器人每天搜索多少次 衡水外呼線路解決 艾比利外呼系統(tǒng)

win 需要先安裝luaforwindows
linux 需要安裝 luarocks 并 luarocks install luasql-mysql

require"luasql.mysql"
 
--創(chuàng)建環(huán)境對(duì)象
env=luasql.mysql()
 
--連接數(shù)據(jù)庫(kù)
conn=env:connect("數(shù)據(jù)庫(kù)名","用戶名","密碼","IP地址",端口)
 
--設(shè)置數(shù)據(jù)庫(kù)的編碼格式
conn:execute"SET NAMES GB2312"
 
--執(zhí)行數(shù)據(jù)庫(kù)操作
cur=conn:execute("select * from role")
 
row=cur:fetch({},"a")
 
while row do
var=string.format("%d%s\n",row.id,row.name)
 
print(var)
 
row=cur:fetch(row,"a")
end
 
conn:close()--關(guān)閉數(shù)據(jù)庫(kù)連接
env:close()--關(guān)閉數(shù)據(jù)庫(kù)環(huán)境

上面是個(gè)簡(jiǎn)單的,我們?cè)賮?lái)看個(gè)稍微復(fù)雜些的例子

-- load driver 
require "luasql.mysql" 
-- create environment object 
env = assert (luasql.mysql()) 
-- connect to data source 
con = assert (env:connect("database", "usr", "password", "192.168.xx.xxx", 3306)) 
-- reset our table 
res = con:execute"DROP TABLE people"        --建立新表people 
res = assert (con:execute[[ 
CREATE TABLE people(        
  name varchar(50), 
  email varchar(50) 
) 
]]) 
-- add a few elements 
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", }, 
} 
for i, p in pairs (list) do                      --加入數(shù)據(jù)到people表 
res = assert (con:execute(string.format([[ 
  INSERT INTO people                      
  VALUES ('%s', '%s')]], p.name, p.email) 
)) 
end 
-- retrieve a cursor 
cur = assert (con:execute"SELECT name, email from people")  --獲取數(shù)據(jù) 
-- print all rows 
row = cur:fetch ({}, "a") -- the rows will be indexed by field names  --顯示出來(lái) 
while row do 
print(string.format("Name: %s, E-mail: %s", row.name, row.email)) 
row = cur:fetch (row, "a") -- reusing the table of results 
end 
-- close everything 
cur:close() 
con:close() 
env:close() 

您可能感興趣的文章:
  • 在Lua程序中使用MySQL的教程

標(biāo)簽:營(yíng)口 泰安 臨滄 南京 昌都 西寧 宿遷

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《簡(jiǎn)單的Lua 連接操作mysql數(shù)據(jù)庫(kù)的方法》,本文關(guān)鍵詞  簡(jiǎn)單,的,Lua,連接,操作,mysql,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《簡(jiǎn)單的Lua 連接操作mysql數(shù)據(jù)庫(kù)的方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于簡(jiǎn)單的Lua 連接操作mysql數(shù)據(jù)庫(kù)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    微博| 福州市| 建阳市| 高台县| 镇江市| 潞城市| 海盐县| 白水县| 惠水县| 蓬安县| 云霄县| 乃东县| 济源市| 开原市| 保山市| 澳门| 禹城市| 建德市| 唐海县| 莲花县| 辉县市| 闽清县| 太仆寺旗| 新野县| 嵊州市| 建瓯市| 四平市| 龙岩市| 南京市| 西乡县| 潜江市| 宜城市| 天长市| 太白县| 竹溪县| 新宾| 柳州市| 句容市| 河南省| 陆丰市| 潮州市|