require'mysql' begin db = Mysql.init db.options(Mysql::SET_CHARSET_NAME, 'utf8') db = Mysql.real_connect("127.0.0.1", "root", "123456", "test", 3306) db.query("SET NAMES utf8") db.query("drop table if exists tb_test") db.query("create table tb_test (id int, text LONGTEXT) ENGINE=MyISAM DEFAULT CHARSET=utf8") db.query("insert into tb_test (id, text) values ( 1,'first line'),(2,'second line')") printf "%d rows were inserted\n",db.affected_rows rslt = db.query("select text from tb_test") while row = rslt.fetch_row do puts row[0] end rescueMysql::Error => e puts "Error code: #{e.errno}" puts "Error message: #{e.error}" puts "Error SQLSTATE: #{e.sqlstate}"if e.respond_to?("sqlstate") ensure db.close if db end
defwriteToFile(file,content) fp = File.new(file,"a+") if fp fp.syswrite(content) else puts "..." end end
defconnect(host) redis = Redis.new(:host => host,:port => 6379) redis.info.keys.each do |key| puts "#{key}:\t"+redis.info["#{key}"] end end
connect("1.1.1.1")
sqlite3
1 2 3 4 5 6 7 8 9 10 11 12
require'sqlite3'
db = SQLite3::Database.new('test.db')
db.execute("create table test( ID integet not null, Username varchar(20) null, Password varchar(64) null)") db.execute("insert into test(ID.Username,Password) values('0','admin','admin')") db.execute("select * from test") db.execute("update test set password='12345' where id=0")
3.Ruby socket
服务端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require'socket'
server = TCPServer.open('0.0.0.0', 8080) loop do Thread.start(server.accept) do |client| begin whiletrue puts "#{client.to_i} online" data = client.read() throw"empty"if data.empty? #puts data.length puts data end rescueException => e puts "#{client.to_i} offline" end end end
客户端:
1 2 3 4 5 6 7 8 9
require'socket'
hostname = '127.0.0.1' port = 8080 buf = 'test' s = TCPSocket.open(hostname, port) s.write buf sleep(1) s.close