国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

這里是將一個csv的表導入到數據庫中,用lua解析

2024-07-21 02:51:26
字體:
來源:轉載
供稿:網友
PRint(os.date())--dofile("C:/Program Files (x86)/Lua/lua_script/sql_str.lua") local ss = "tblTreasureInfo"local file = "I:/PlanJ_SS/SS_Server/dbcsv/ssdbGame/" .. ss ..".csv"local sql_del = "delete from "..sslocal result_ = "QA已導好表,執行文件  " .. ss .."  成功?。。?!"local Zi_duan = {}local sql_Str = nil function getTpl0(tname)     -- 獲取表各個字段		local t = {}	table.insert(t, Zi_duan)    for k, v in ipairs(t) do        if k == 1 then            return t[k]    --返回一個table        end    endend--以上的功能,可以打印第一條數據的字段function LoadCSV(filePath)    -- Load File        local file = assert(io.open(filePath, 'r'))        local content = file:read("*all")        file:close()    -- Read Word One By One        local c         = content -- content        local p         = 0       -- content check pos        local w         = ""      -- content check word        local w_next    = ""      -- content check word(next one)        local x         = 1       -- content table x        local y         = 1       -- content table y                local opt       = {}      -- output table              opt[y]    = {}              opt[y][x] = ""        local in_quote  = false   -- quote mode        while true do            p      = p + 1            w      = string.sub(c,p  ,p  )            w_next = string.sub(c,p+1,p+1)            if w == ',' then                if in_quote then                    opt[y][x] = opt[y][x]..w                else                    x = x + 1                    opt[y][x] = ""                end            elseif w == '"' then                if in_quote then                    if w_next == '"' then                        opt[y][x] = opt[y][x]..w                        p = p + 1                    else                        in_quote = false                    end                else                    in_quote = true                end            elseif w == '/n' then                if in_quote then                    opt[y][x] = opt[y][x]..w                else                    y = y + 1                    x = 1                    opt[y] = {}                    opt[y][x] = ""                end            elseif w == "" or w == nil then                opt[y][x] = nil                break            else                opt[y][x] = opt[y][x]..w            end        end    -- Return Table        return optend    local csvData = LoadCSV(file)    for k,v in pairs(csvData) do		--local result = table.concat(v)		--print(result)		if k == 1 then			for k2,v2 in pairs(v) do				if k == 1 then 					--print(k.."===="..k2.."---》"..v2)					table.insert(Zi_duan, v2)				end			end			table.remove(csvData, 1)			break;					else		end    end						--[[    for k,v in pairs(csvData) do							for k2,v2 in pairs(v) do									table.insert(t_tt[k], v2)												end     end	--]]function createInsertSql(tname, tpl)   --插入一個文件名和一個table    local tpl0 = getTpl0(tname)     -- 獲取表各個字段    local ret = {}                  -- 插入記錄sql     table.insert(ret, string.format("insert into `%s`(", tname))    for k, v in pairs(tpl0) do        if k > 1 then            table.insert(ret, ",")        end        table.insert(ret, "`")        table.insert(ret, v)		table.insert(ret, "`")    end	table.insert(ret, ") values ")	local result = table.concat(ret)    -- 最終的sql語句	--insert into `tpl_pack`(`packId`,`itemId`,``group``,`num`,`rate`,`rateType`) values 	print(result)		    for k, v in pairs(tpl) do         --解析一個table		if k <= (#tpl-1) then        if k > 1 then            table.insert(ret, ",")        end        table.insert(ret, "(")        for k2, v2 in pairs(v) do            if k2 > 1 then                table.insert(ret, ",")            end            if v2 == "nil_1" then                table.insert(ret, "'")				table.insert(ret, "'")							elseif type(v2) == "string" then				table.insert(ret, string.format("'%s'", v2))            else				table.insert(ret, "'")                table.insert(ret, v2)				table.insert(ret, "'")            end        end        table.insert(ret, ")")		end    end    table.insert(ret, ";")     local result = table.concat(ret)    -- 最終的sql語句		sql_Str = result    print(result)	--print(result_)    --print()endcreateInsertSql(ss, csvData)   --這里寫上路徑和文件名--執行數據庫require"luasql.MySQL"   --創建環境對象   env=luasql.mysql()    --連接數據庫   --conn=env:connect("wuxinchang","root","root","127.0.0.1",3306)    --設置數據庫的編碼格式   conn=env:connect("ssdbGame","derek","derek123","192.168.3.236",3306)    --設置數據庫的編碼格式   --conn:execute"SET NAMES UTF8"   --執行數據庫操作conn:execute"SET NAMES GB2312"   --執行數據庫操作        status,errorString = conn:execute(sql_del)      status,errorString = conn:execute(sql_Str)      if errorString == nil then  	print(result_)    return 1;  else      print(status,errorString )  end     conn:close()--關閉數據庫連接   env:close()--關閉數據庫環境  --dofile("C:/Program Files (x86)/Lua/lua_script/sql_str.lua") 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湘西| 若尔盖县| 翼城县| 车致| 台东县| 武威市| 纳雍县| 肥东县| 新源县| 汤原县| 香格里拉县| 呼图壁县| 万荣县| 杭锦后旗| 定远县| 公安县| 内丘县| 榆林市| 山阳县| 凤阳县| 泸西县| 无锡市| 务川| 津南区| 松溪县| 兴安县| 平定县| 盘锦市| 海安县| 哈尔滨市| 荥阳市| 鲁甸县| 永善县| 广东省| 勃利县| 乐陵市| 夹江县| 林芝县| 宜都市| 德化县| 吐鲁番市|