# Faild to update sqlite database using activerecord

**URL:** <https://www.ruby-forum.com/t/faild-to-update-sqlite-database-using-activerecord/173227>\
**Category:** Rails\
**Created:** [August 5, 2009, 9:56am UTC](https://www.ruby-forum.com/t/faild-to-update-sqlite-database-using-activerecord/173227 "2009-08-05T09:56:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@pierr](https://www.ruby-forum.com/u/pierr)\
**Post date:** [August 5, 2009, 9:56am UTC](https://www.ruby-forum.com/t/faild-to-update-sqlite-database-using-activerecord/173227/1 "2009-08-05T09:56:20Z")

</div>

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  
|¢¸¢ó¹Þ¤ëûL1B2nrDJ0é¸?6I!A¤óÉÍ·¢!!«êÞó¿óEg!A?  
B?L1B2¬Jké¹|¤óÉÍ·¢ý«êÞó¿óEgúJ6Ah,@d(:?}³ìÞÇ?\>@7\<TÏ@i?MòD6¹úL1B2M;OBòL;X?c\<T10?M?|é¸?  
I r;O a??c-DJNJ3F.rDI&?

–\>after modify before save  
hello  
world

–\>after save  
72  
|¢¸¢ó¹Þ¤ëûL1B2nrDJ0é¸?6I!A¤óÉÍ·¢!!«êÞó¿óEg!A?  
B?L1B2¬Jké¹|¤óÉÍ·¢ý«êÞó¿óEgúJ6Ah,@d(:?}³ìÞÇ?\>@7\<TÏ@i?MòD6¹úL1B2M;OBòL;X?c\<T10?M?|é¸?  
Ir;Oa??c-DJNJ3F.rDI&?

---

<div class="post-metadata">

**Author:** ![pierr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@pierr](https://www.ruby-forum.com/u/pierr)\
**Post date:** [August 5, 2009, 9:59am UTC](https://www.ruby-forum.com/t/faild-to-update-sqlite-database-using-activerecord/173227/2 "2009-08-05T09:59:15Z")

</div>

On Aug 5, 8:44 am, pierr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![pierr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@pierr](https://www.ruby-forum.com/u/pierr)\
**Post date:** [August 5, 2009, 10:16am UTC](https://www.ruby-forum.com/t/faild-to-update-sqlite-database-using-activerecord/173227/3 "2009-08-05T10:16:49Z")

</div>

I forgot to specify the primary key as rowid.

Thanks , Fred.

On Aug 5, 3:58 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
