Hi all,
I was trying to update the JIS-encoded text in the databse to UTF-8
text but failed (For simplicity, I use "hello" & "world"
in the test
program below). Please point me the mistake I made here. Thanks.
#using sqlite
ActiveRecord::Base::establish_connection
(:adapter=>"sqlite3",:dbfile=>"the.db")
#try to do the update but failed
rowid = 72 #it is the default unique ID in sqlite
Basic.find(:all , :select=>"rowid , event_name ,text ",
:conditions=>["rowid = ?",rowid]).each do |o|
puts "before modify"
puts o.event_name
puts o.text
o.event_name = "hello"
o.text = "world"
puts "after modify before save"
puts o.event_name
puts o.text
o.save
#o.update_attribute(:event_name,"hello")
end
#confirm
Basic.find(:all , :select=>"rowid , event_name ,text ",
:conditions=>["rowid =
?",rowid]).each
do |o|
puts "after save"
puts o.rowid
puts o.event_name
puts o.text
end
output Log :
-->before modify
|⒏Ⅲ罐る鸏1B2nrDJ0楦?6I!Aん赏发!!摅矿Eg!A?
B?L1B2琂k楣|ん赏发贽罂驟g鶭6Ah,@d(:?}踌耷?>@7<T螥i?M駾6国L1B2M;OB騆\;X?c<T10?M?|楦?
I r;O a??c-DJNJ3F.rDI&?
-->after modify before save
hello
world
-->after save
72
|⒏Ⅲ罐る鸏1B2nrDJ0楦?6I!Aん赏发!!摅矿Eg!A?
B?L1B2琂k楣|ん赏发贽罂驟g鶭6Ah,@d(:?}踌耷?>@7<T螥i?M駾6国L1B2M;OB騆\;X?c<T10?M?|楦?
Ir;Oa??c-DJNJ3F.rDI&?
Frederick Cheung
2009-Aug-05 07:58 UTC
Re: Faild to update sqlite database using activerecord
On Aug 5, 8:44 am, pierr <pierr.c...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Basic.find(:all , :select=>"rowid , event_name ,text ", > :conditions=>["rowid = ?",rowid]).each do |o|Is rowid your primary key (and if so have you told active record that it is) ? if not then your save is failing because you haven''t selected the primary key. Also check that you don''t have any failing validations Fred> > puts "before modify" > puts o.event_name > puts o.text > o.event_name = "hello" > o.text = "world" > puts "after modify before save" > puts o.event_name > puts o.text > o.save > #o.update_attribute(:event_name,"hello") > end > #confirm > Basic.find(:all , :select=>"rowid , event_name ,text ", > :conditions=>["rowid = ?",rowid]).each > do |o| > puts "after save" > puts o.rowid > puts o.event_name > puts o.text > end > > output Log : > > -->before modify > |⒏Ⅲ罐る鸏1B2nrDJ0楦?6I!Aん赏发!!摅矿Eg!A? > B?L1B2琂k楣|ん赏发贽罂驟g鶭6Ah,@d(:?}踌耷?>@7<T螥i?M駾6国L1B2M;OB騆\;X?c<T10?M?|楦? > I r;O a??c-DJNJ3F.rDI&? > > -->after modify before save > hello > world > > -->after save > 72 > |⒏Ⅲ罐る鸏1B2nrDJ0楦?6I!Aん赏发!!摅矿Eg!A? > B?L1B2琂k楣|ん赏发贽罂驟g鶭6Ah,@d(:?}踌耷?>@7<T螥i?M駾6国L1B2M;OB騆\;X?c<T10?M?|楦? > Ir;Oa??c-DJNJ3F.rDI&?
I forgot to specify the primary key as rowid. Thanks , Fred. On Aug 5, 3:58 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 5, 8:44 am, pierr <pierr.c...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Basic.find(:all , :select=>"rowid , event_name ,text ", > > :conditions=>["rowid = ?",rowid]).each do |o| > > Is rowid your primary key (and if so have you told active record that > it is) ? if not then your save is failing because you haven''t selected > the primary key. Also check that you don''t have any failing > validations > > Fred > > > > > puts "before modify" > > puts o.event_name > > puts o.text > > o.event_name = "hello" > > o.text = "world" > > puts "after modify before save" > > puts o.event_name > > puts o.text > > o.save > > #o.update_attribute(:event_name,"hello") > > end > > #confirm > > Basic.find(:all , :select=>"rowid , event_name ,text ", > > :conditions=>["rowid = ?",rowid]).each > > do |o| > > puts "after save" > > puts o.rowid > > puts o.event_name > > puts o.text > > end > > > output Log : > > > -->before modify > > |⒏Ⅲ罐る鸏1B2nrDJ0楦?6I!Aん赏发!!摅矿Eg!A? > > B?L1B2琂k楣|ん赏发贽罂驟g鶭6Ah,@d(:?}踌耷?>@7<T螥i?M駾6国L1B2M;OB騆\;X?c<T10?M?|楦? > > I r;O a??c-DJNJ3F.rDI&? > > > -->after modify before save > > hello > > world > > > -->after save > > 72 > > |⒏Ⅲ罐る鸏1B2nrDJ0楦?6I!Aん赏发!!摅矿Eg!A? > > B?L1B2琂k楣|ん赏发贽罂驟g鶭6Ah,@d(:?}踌耷?>@7<T螥i?M駾6国L1B2M;OB騆\;X?c<T10?M?|楦? > > Ir;Oa??c-DJNJ3F.rDI&?