如何将(sql)数据库添加到我的sintra应用程序?

时间:2021-12-02 20:00:36

When I started to code my sinatra application I never used it before. Note that I had and still have no experience with RoR. I had one .rb file and one .haml and was happy. Now I had to split .rb file into about 10 'library' files as the whole application gets more and more complex.

当我开始编写我的sinatra应用程序时,我以前从未使用它。请注意,我已经并且仍然没有使用过RoR的经验。我有一个.rb文件和一个.haml,很高兴。现在我不得不将.rb文件拆分成大约10个“库”文件,因为整个应用程序变得越来越复杂。

I store some application logs/info in csv files and now I am getting conflicts when accessing the csv file. So I think that I need to introduce "proper" database solution. I want it to be part of my ruby (sinatra) application.

我将一些应用程序日志/信息存储在csv文件中,现在我在访问csv文件时遇到了冲突。所以我认为我需要引入“适当的”数据库解决方案。我希望它成为我的ruby(sinatra)应用程序的一部分。

How can I introduce 'light' sql database into my sintra application?

如何将'轻'sql数据库引入我的sintra应用程序?


I am on ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32] soon upgrading to 1.9 (hopefully)

我在ruby 1.8.7(2010-08-16 patchlevel 302)[i386-mingw32]很快升级到1.9(希望如此)

4 个解决方案

#1


2  

I'd recommend looking at Sequel. It's very flexible and powerful, and works well with SQLite, MySQL, Postgres, Oracle and other DBMs. It's not opinionated about how you talk to the database; You can use it as an ORM or with simple datasets, and allows embedded SQL or more programmatic approaches.

我建议看Sequel。它非常灵活和强大,适用于SQLite,MySQL,Postgres,Oracle和其他DBM。关于你如何与数据库交谈并不是自以为是;您可以将其用作ORM或简单数据集,并允许嵌入式SQL或更多编程方法。

#2


1  

For ORM, both ActiveRecord and Sequel are recommended. About database, I guess sqlite3 will be good enough for your need. Also you can choose mysql or pg.

对于ORM,建议使用ActiveRecord和Sequel。关于数据库,我想sqlite3将足以满足您的需求。你也可以选择mysql或pg。

If you want to use active_record, you'll find this article very useful. And if Sequel is the choice, just read Sequel documents here.

如果你想使用active_record,你会发现这篇文章非常有用。如果Sequel是选择,那么请阅读Sequel文档。

After the gems installed. You can start writing some code to connect the db. Then maybe some migration task to build database tables (and don't forget build some corresponding models). Both gem have similar syntax for migrations. After that, import your csv data and well done.

安装宝石后。您可以开始编写一些代码来连接数据库。然后可能会有一些迁移任务来构建数据库表(并且不要忘记构建一些相应的模型)。两个gem都有类似的迁移语法。之后,导入您的csv数据并做得很好。

#3


0  

I have had no trouble using either Active Record or DataMapper to add object persistence to my Sinatra apps. People also tell me Sequel is very good but philosophically it is not not worlds apart from Active Record imho.

我使用Active Record或DataMapper为我的Sinatra应用程序添加对象持久性没有任何问题。人们还告诉我Sequel非常好,但哲学上它并不是与Active Record imho不同的世界。

Active Record and Sequel favour a more database-centric approach, whereby you spell out your tables as a set of database and table definitions in a collection of migration files and merge them into a schema which is then used to build or update your database tables. If you really care about the underlying SQL database then one of these is for you. I find them to be six of one, a half-dozen the other.

活动记录和Sequel赞成更多的数据库为中心的方法,因此你拼出你的表,因为迁移文件的集合在一组数据库和表定义,并将它们合并成一个模式,然后将其用于建立或更新您的数据库表。如果您真的关心底层SQL数据库,那么其中一个适合您。我发现它们是六个,另一个是六个。

DataMapper is more object-centric and lets you define the properties and object relationships you need in your object's own class definition; and then when your app launches you make sure you call DataMapper.auto_upgrade! and it upgrades the database to suit your object graph. The upside is that you only have one place to look to find what properties your object might have. The downside is you have less control over the specifics of the underlying databases, though it's not impossible to tightly define the mappings, DataMapper works well when you care about object graphs over database tables.

DataMapper更加以对象为中心,允许您在对象自己的类定义中定义所需的属性和对象关系;然后当您的应用程序启动时,请确保调用DataMapper.auto_upgrade!它会升级数据库以适合您的对象图。好处是你只有一个地方可以找到你的对象可能具有的属性。缺点是您对底层数据库的细节控制较少,尽管紧密定义映射并非不可能,当您关心数据库表上的对象图时,DataMapper可以很好地工作。

The good news is they pretty much all work in the same way once you have your mappings from object graph to SQL database tables defined. All support lazy or pre-emptive loading of related collections of objects, many_to_many relationships, polymorphism, etc, and tend to vary only in configuration and seeding details.

好消息是,一旦您从对象图到定义的SQL数据库表进行映射,它们几乎都以相同的方式工作。所有这些都支持对相关的对象集合,many_to_many关系,多态等等进行惰性或先发制人加载,并且仅在配置和种子细节方面有所不同。

I often start projects using DataMapper just for its speed of throwing up and tearing down database schemas, as the app's object graph is still in flux; I refactor quickly to use Active Record when the schema has settled down. Next project I think I'll give Sequel a go though, as people do seem to rave about it.

我经常使用DataMapper启动项目只是为了提高和拆除数据库模式的速度,因为应用程序的对象图仍然在不断变化;当架构稳定下来时,我会快速重构以使用Active Record。下一个项目,我想我会给Sequel一个机会,因为人们似乎对此赞不绝口。

#4


0  

I have had success using datamapper with Sinatra, put like the other post you can also use Sequel and Active Record. One advantage to maybe using Active Record though is if you do ever want to use/learn ROR, Active Record is the default ORM so that might be something that you want to consider.

我已经成功使用了与Sinatra的datamapper,就像其他帖子一样,你也可以使用Sequel和Active Record。可能使用Active Record的一个优点是,如果您确实想要使用/学习ROR,则Active Record是默认的ORM,因此可能是您想要考虑的内容。

If you don't want to go the ORM route you can always use the SQL-Ruby gem which will allow you to create and run sql queries. Here is some example code from the website http://sqlite-ruby.rubyforge.org/

如果您不想使用ORM路由,则可以始终使用SQL-Ruby gem,这将允许您创建和运行SQL查询。以下是网站http://sqlite-ruby.rubyforge.org/上的一些示例代码

require 'sqlite'

db = SQLite::Database.new( "data.db" )

db.execute( "select * from table" ) do |row|
  p row
end

db.close

#1


2  

I'd recommend looking at Sequel. It's very flexible and powerful, and works well with SQLite, MySQL, Postgres, Oracle and other DBMs. It's not opinionated about how you talk to the database; You can use it as an ORM or with simple datasets, and allows embedded SQL or more programmatic approaches.

我建议看Sequel。它非常灵活和强大,适用于SQLite,MySQL,Postgres,Oracle和其他DBM。关于你如何与数据库交谈并不是自以为是;您可以将其用作ORM或简单数据集,并允许嵌入式SQL或更多编程方法。

#2


1  

For ORM, both ActiveRecord and Sequel are recommended. About database, I guess sqlite3 will be good enough for your need. Also you can choose mysql or pg.

对于ORM,建议使用ActiveRecord和Sequel。关于数据库,我想sqlite3将足以满足您的需求。你也可以选择mysql或pg。

If you want to use active_record, you'll find this article very useful. And if Sequel is the choice, just read Sequel documents here.

如果你想使用active_record,你会发现这篇文章非常有用。如果Sequel是选择,那么请阅读Sequel文档。

After the gems installed. You can start writing some code to connect the db. Then maybe some migration task to build database tables (and don't forget build some corresponding models). Both gem have similar syntax for migrations. After that, import your csv data and well done.

安装宝石后。您可以开始编写一些代码来连接数据库。然后可能会有一些迁移任务来构建数据库表(并且不要忘记构建一些相应的模型)。两个gem都有类似的迁移语法。之后,导入您的csv数据并做得很好。

#3


0  

I have had no trouble using either Active Record or DataMapper to add object persistence to my Sinatra apps. People also tell me Sequel is very good but philosophically it is not not worlds apart from Active Record imho.

我使用Active Record或DataMapper为我的Sinatra应用程序添加对象持久性没有任何问题。人们还告诉我Sequel非常好,但哲学上它并不是与Active Record imho不同的世界。

Active Record and Sequel favour a more database-centric approach, whereby you spell out your tables as a set of database and table definitions in a collection of migration files and merge them into a schema which is then used to build or update your database tables. If you really care about the underlying SQL database then one of these is for you. I find them to be six of one, a half-dozen the other.

活动记录和Sequel赞成更多的数据库为中心的方法,因此你拼出你的表,因为迁移文件的集合在一组数据库和表定义,并将它们合并成一个模式,然后将其用于建立或更新您的数据库表。如果您真的关心底层SQL数据库,那么其中一个适合您。我发现它们是六个,另一个是六个。

DataMapper is more object-centric and lets you define the properties and object relationships you need in your object's own class definition; and then when your app launches you make sure you call DataMapper.auto_upgrade! and it upgrades the database to suit your object graph. The upside is that you only have one place to look to find what properties your object might have. The downside is you have less control over the specifics of the underlying databases, though it's not impossible to tightly define the mappings, DataMapper works well when you care about object graphs over database tables.

DataMapper更加以对象为中心,允许您在对象自己的类定义中定义所需的属性和对象关系;然后当您的应用程序启动时,请确保调用DataMapper.auto_upgrade!它会升级数据库以适合您的对象图。好处是你只有一个地方可以找到你的对象可能具有的属性。缺点是您对底层数据库的细节控制较少,尽管紧密定义映射并非不可能,当您关心数据库表上的对象图时,DataMapper可以很好地工作。

The good news is they pretty much all work in the same way once you have your mappings from object graph to SQL database tables defined. All support lazy or pre-emptive loading of related collections of objects, many_to_many relationships, polymorphism, etc, and tend to vary only in configuration and seeding details.

好消息是,一旦您从对象图到定义的SQL数据库表进行映射,它们几乎都以相同的方式工作。所有这些都支持对相关的对象集合,many_to_many关系,多态等等进行惰性或先发制人加载,并且仅在配置和种子细节方面有所不同。

I often start projects using DataMapper just for its speed of throwing up and tearing down database schemas, as the app's object graph is still in flux; I refactor quickly to use Active Record when the schema has settled down. Next project I think I'll give Sequel a go though, as people do seem to rave about it.

我经常使用DataMapper启动项目只是为了提高和拆除数据库模式的速度,因为应用程序的对象图仍然在不断变化;当架构稳定下来时,我会快速重构以使用Active Record。下一个项目,我想我会给Sequel一个机会,因为人们似乎对此赞不绝口。

#4


0  

I have had success using datamapper with Sinatra, put like the other post you can also use Sequel and Active Record. One advantage to maybe using Active Record though is if you do ever want to use/learn ROR, Active Record is the default ORM so that might be something that you want to consider.

我已经成功使用了与Sinatra的datamapper,就像其他帖子一样,你也可以使用Sequel和Active Record。可能使用Active Record的一个优点是,如果您确实想要使用/学习ROR,则Active Record是默认的ORM,因此可能是您想要考虑的内容。

If you don't want to go the ORM route you can always use the SQL-Ruby gem which will allow you to create and run sql queries. Here is some example code from the website http://sqlite-ruby.rubyforge.org/

如果您不想使用ORM路由,则可以始终使用SQL-Ruby gem,这将允许您创建和运行SQL查询。以下是网站http://sqlite-ruby.rubyforge.org/上的一些示例代码

require 'sqlite'

db = SQLite::Database.new( "data.db" )

db.execute( "select * from table" ) do |row|
  p row
end

db.close