I have an app that uses SQLite now. And now I am changing it to PostgreSQL. The table structure is changed. But I want to import the data from the previous db to the new one.
我现在有一个使用SQLite的应用。现在我把它改成PostgreSQL。更改了表结构。但是我想要将前一个db的数据导入到新的数据库中。
How can I fetch the data and save it in the new table?
如何获取数据并将其保存到新表中?
I tried few things like getting the json from url and save the data in the table but I have problems in parsing the json string. So I am confused on how to proceed.
我尝试过从url获取json并将数据保存到表中,但在解析json字符串时遇到了问题。所以我对如何进行感到困惑。
2 个解决方案
#1
0
You can export the data from sqlite into a sql file with a method in your model. This way you can change table column names since table structure changed.
您可以使用模型中的方法将来自sqlite的数据导出到sql文件中。通过这种方式,可以更改表列名称,因为表结构发生了更改。
sql_export method in app/models/user.rb
:
应用程序/模型/ user.rb sql_export方法:
class User < ActiveRecord::Base
def self.sql_export
filecontent = ""
User.all.each do |user|
filecontent << "INSERT INTO users (email, password) VALUES ('#{user.email}','#{user.password}');"
end
File.open("tmp/export.sql", "w+") do |f|
f.write(filecontent)
end
end
Fire up rails console and run the method (Make sure that you are running in the environment that it connects to sqlite database):
启动rails控制台并运行该方法(请确保您正在其连接到sqlite数据库的环境中运行):
$> rails c
$ irb> User.sql_export
It basically read all data from your database and generate insert statements and write to tmp/export.sql
file.
它基本上读取数据库中的所有数据,并生成insert语句并写入tmp/export。sql文件。
Then import the sql file to your postgresql
然后将sql文件导入到postgresql
psql databasename < tmp/export
#2
0
I have now found a way to parse the json data as follows
我现在找到了一种解析json数据的方法,如下所示
url = "http://samplesite/export_data.json"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
post_data = JSON.parse(data)
post_data.each do |value|
u = Model.new
u.field = value['name']
u.save
end
This can be used to take data from table and can be saved into another table accordingly.
这可以用于从表中获取数据,并相应地保存到另一个表中。
#1
0
You can export the data from sqlite into a sql file with a method in your model. This way you can change table column names since table structure changed.
您可以使用模型中的方法将来自sqlite的数据导出到sql文件中。通过这种方式,可以更改表列名称,因为表结构发生了更改。
sql_export method in app/models/user.rb
:
应用程序/模型/ user.rb sql_export方法:
class User < ActiveRecord::Base
def self.sql_export
filecontent = ""
User.all.each do |user|
filecontent << "INSERT INTO users (email, password) VALUES ('#{user.email}','#{user.password}');"
end
File.open("tmp/export.sql", "w+") do |f|
f.write(filecontent)
end
end
Fire up rails console and run the method (Make sure that you are running in the environment that it connects to sqlite database):
启动rails控制台并运行该方法(请确保您正在其连接到sqlite数据库的环境中运行):
$> rails c
$ irb> User.sql_export
It basically read all data from your database and generate insert statements and write to tmp/export.sql
file.
它基本上读取数据库中的所有数据,并生成insert语句并写入tmp/export。sql文件。
Then import the sql file to your postgresql
然后将sql文件导入到postgresql
psql databasename < tmp/export
#2
0
I have now found a way to parse the json data as follows
我现在找到了一种解析json数据的方法,如下所示
url = "http://samplesite/export_data.json"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
post_data = JSON.parse(data)
post_data.each do |value|
u = Model.new
u.field = value['name']
u.save
end
This can be used to take data from table and can be saved into another table accordingly.
这可以用于从表中获取数据,并相应地保存到另一个表中。