‘#{user_id}’,的单引号别忘了,查了一下午,脚本语言不用管类型的方便让我忘了这个
require 'mysql2'
require "uuidtools"
require 'json'
require 'pp'
require 'chinese_pinyin'
departlist={}
client = Mysql2::Client.new(:host => "192.168.0.161",
:username => "root",
:password =>"test",
:database =>"oa",
:encodidng=>"utf8")
results = client.query("SELECT * FROM oa_department")
results.each do |x|
departlist[x["DEPARTMENT_ID"]]=x["NAME"]
end
# puts departlist
# id=UUIDTools::UUID.random_create.to_s.gsub('-','')
# puts id
dbuse=[
{"no":100,"name":"张山","tel":11111111111,"mail":"zs@test.com","mark":"副总","depart":"人力资源部","sex":"F","add":"杭州"},
{"no":130,"name":"李四","tel":122222222222,"mail":"ls@test.com","mark":"员工","depart":"财务部","sex":"F","add":"杭州"},
].to_json
tj=JSON.parse(dbuse)
# pp tj
tj.each do |a|
staff_id=UUIDTools::UUID.random_create.to_s.gsub('-','')
aname=a["name"]
name_en=Pinyin.t(a["name"]).gsub(' ','')
bianma= a["no"]
department_id=departlist.key(a["depart"])
functions="后补"
tel= a["tel"]
email= a["mail"].gsub(' ','')
sex=a["sex"]
birthday="1000-00-00"
nation="汉族"
jobtype=a["mark"]
jobjointime="0000-00-00"
faddress="后补"
political="后补"
pjointime="0000-00-00"
sfid="后补"
marital="后补"
djointime="2000-00-00"
post=a["mark"]
pojointime="2000-00-00"
education="后补"
shool="后补"
major="后补"
ftitle="后补"
certificate="后补"
contractlength=3
cstartime="2019-01-01"
csendtime="2022-01-01"
address=a["add"]
user_id=Pinyin.t(a["name"]).gsub(' ','')
bz=a["mark"]
sql="INSERT INTO `oa_staff`VALUES (
'#{staff_id}',
'#{aname}',
'#{name_en}',
'#{bianma}',
'#{department_id}',
'#{functions}',
'#{tel}',
'#{email}',
'#{sex}',
'#{birthday}',
'#{nation}',
'#{jobtype}',
'#{jobjointime}',
'#{faddress}',
'#{political}',
'#{pjointime}',
'#{sfid}',
'#{marital}',
'#{djointime}',
'#{post}',
'#{pojointime}',
'#{education}',
'#{shool}',
'#{major}',
'#{ftitle}',
'#{certificate}',
#{contractlength},
'#{cstartime}',
'#{csendtime}',
'#{address}',
'#{user_id}',
'#{bz}')"
puts sql
client.query(sql)
end#