Hyung Kyu Park
Hyung Kyu Park

Reputation: 265

How to store a simple string array in SQLite 3?

I want to do some testing...

If there is an array, array = [a, b, c, d], I want to save it in my database (SQLite 3) and call the elements when a user types 'array' in the input area.

I'm using cloud9 for testing! In 'migrates' folder, there is a file that has this code.

class CreateFans < ActiveRecord::Migration
  def change
    create_table :fans do |t|
      t.string :content


      t.timestamps null: false
    end
  end
end

I know I have to use this page for defining variable names of database data. but where should I put on my 'array' data?

The conclusion is...

I want to know how to save and recall an array in my database!

Upvotes: 0

Views: 1080

Answers (1)

Saloaty
Saloaty

Reputation: 26

You could convert the array into a string to store it in the database. When you pull it from the database, convert it back into an array.

myModel.myDbObject = array.join("")

Then when pulling from the database...

array[] = myModel.myDbObject.split("")

That's essentially what I had to do for one of my Ruby on Rails applications.

Or for array elements longer than just one letter

myModel.myDbObject = array.join(",")

Pulling from database

array[] = myModel.myDbObject = array.split(",")

Upvotes: 1

Related Questions