O’zbekiston aloqa va axborotlashtirish agentligi
Rubotoda SQLite malumotlar ombori bilan ishlash
Download 0.58 Mb. Pdf ko'rish
|
ruby dasturlash tili asoslari
Rubotoda SQLite malumotlar ombori bilan ishlash
Androidda SQLite malumotlar bazidan foydalaniladi. Bu imkoniyatlar foydalanish uchun Ruby dasturlash tili foydalanishi uchun SQLDroid dasturi talab qilinadi. Dasturni https://github.com/SQLDroid/SQLDroid manzilidan yuklab olish o’rnatish kerak. Dasturni hosil qilish buyrug’i ruboto gen app -t 15
--package org.ruboto.example.sqliteda_ishlash Dastur SQLiteni qo’llashi uchun quyidagi buyruq berilishi kerak source "http://rubygems.org" gem 'activerecord', '<4.0.0' gem 'activerecord-jdbc-adapter', '<1.3.0' gem 'activerecord-jdbcsqlite3-adapter', '<1.3.0' gem 'sqldroid' Dastur papkasida yangi fayl yaratamiz src/android_logger.rb class AndroidLogger def self.debug(*args) # Java log yaratildi::android.util.Log.d *args puts *args end def self.debug? true # funksiya qayta yuklandi end
93 def self.info(*args) # Java info log yaratildi::android.util.Log.i *args puts *args end def self.warn(*args) # Java elon log yaratildi ::android.util.Log.w *args puts *args end
def self.error(*args) # Java xato log yaratildi::android.util.Log.e *args args.each do |m| if m.is_a? Exception puts m.message puts m.backtrace.join("\n") end puts m
end end
end Malumotlar bazasini hosil qilish src/database.rb require 'active_record' require 'android_logger' class Database def self.setup(context) ActiveRecord::Base.logger = AndroidLogger db_dir = "#{context.files_dir.path}/my_db.sqlite" connection_options = { :adapter => 'jdbcsqlite3', :driver => 'org.sqldroid.SQLDroidDriver', :url => "jdbc:sqldroid:#{db_dir}", :database => db_dir, :pool => 30, :timeout => 25000,} ActiveRecord::Base.configurations = { :production => connection_options } 94 ActiveRecord::Base.establish_connection(ActiveRecord:: Base.configurations[:production]) end
def self.migrate(context)src_dir = 'file:' + context.package_manager.getApplicationInfo($package_name, 0). sourceDir + '!/' migration_path = File.expand_path("#{src_dir}/migrate") puts "Looking for migration scripts in #{migration_path}" migrator = ActiveRecord::Migrator.new(:up, migration_path) if migrator.pending_migrations.size > 0 puts "Found #{migrator.pending_migrations.size} migrations." migrator.migrate end
end end
Yuqoridagi kodda dasturning ishchi papkasida my_db.sql malumotlar fayli hosil qilinadi. db_dir = "#{context.files_dir.path}/my_db.sqlite" Malumotlar bazasida yangi jadval hosil qiluvchi fayl yaratish. src/migrate/001_create_people.rb class CreatePeople < ActiveRecord::Migration def self.up create_table :people do |t| t.string :name, :limit => 32, :null => false t.date :birthdate t.timestamps end
add_index :people, :name, :unique => true end
def self.down drop_table :people end end
Yuqoridagi kodlar yordamida people jadvali hosil qilindi. create_table :people do |t| 95 Jadvalda name ustuni elon qilingan va null qiymat qabul qilmaslik 32 kenglikkacha qiymap qabul qilish parametrlari kiritilgan. t.string :name, :limit => 32, :null => false name qiymatlarni bir xil nom bilan qabul qilmaslik sharti kiritlgan. add_index :people, :name, :unique => true Dasturning asosiy kodlari src\sqliteda_ishlash faylidan joy olgan.
|
ma'muriyatiga murojaat qiling