2

Hi I am prepopulating the db as said here http://docs.rhomobile.com/faq#how-to-pre-populate-client-database but I have a problem, that when I make reset DB with default code

def do_reset
    Rhom::Rhom.database_full_reset
    SyncEngine.dosync
    @msg = "Database has been reset."
    redirect :action => :index, :query => {:msg => @msg}
  end

then I am losing the data. How can I make that the prepopulated database alsways will be loaded, when I make reset. Cheers

I come up with such solution

in view do_reset.erb

<% 
Antwort.delete_all()
   file_name = File.join(Rho::RhoApplication::get_model_path('app','Settings'), 'antwort.txt')
   file = File.new(file_name,"r")
   aid=0
   file.each_line("\n") do |row|
     col = row.split("|")
 aid=aid+1
    @antwort=Antwort.create(
       {"aid" => aid, "qid" => col[0],"antwort"=>col[1],"richtig"=>col[2]}
     )

     qty=file.lineno
     break if file.lineno > 3000
   end

   Questions.delete_all()
file_name = File.join(Rho::RhoApplication::get_model_path('app','Settings'), 'questions.txt')
  file = File.new(file_name)

  file.each_line("\n") do |row|
    col = row.split("|")

   @question=Questions.create(
      {"id" => col[0], "question" => col[1],"answered"=>'0',"show"=>'1',"tutorial"=>col[4]}
    )


    break if file.lineno > 1500
  end
file.close
  @msg="OK"
  %>

But only problem I have now is single quotes aka ' in the texts. They are then displayed in app as a triangle with ? inside like � one. What to do?

MR.GEWA
  • 833
  • 1
  • 15
  • 37
  • I think that, in this case, you will need to populate the database using inserts directly. You can create an array of SQL statements and run each one of it. Both in the creation of the prepopulated database as when the database is reset. – Douglas Lise Aug 18 '11 at 11:19
  • you mean instead of SyncEngine.dosync I should write a code which will insert data in database? any idea what will be the best way? CSV? – MR.GEWA Aug 18 '11 at 12:22
  • Yes, it's close this. But, how Geoffrey remembered in his answer, you could use a pipe delimited text, that I think it's a better idea. – Douglas Lise Aug 18 '11 at 14:34

1 Answers1

1

You can seed the database using a pipe delimited text file, as explained here.

So, in the instance you are using the Property Bag model definitions, you will have a file called object_values.txt and load up whichever sources, properties, and values necessary.

Geoffrey
  • 562
  • 4
  • 9
  • i came up to that solution and tried to do that, but I don't know why I was getting an error message about that the last "order" columnt doesn't exist and I have made my own small update script as shown above. Only problem I have is a single quote problem, maybe you could help to solve it out... – MR.GEWA Aug 18 '11 at 21:54