ruby下操作mysql的一个点

‘#{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#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值