我使用 sqlite3 如下:
db = SQLite3::Database.open "data.db"
stm = db.prepare "SELECT * FROM COMPANIES"
rs = stm.execute
while (row = rs.next) do
name = row[1]
Run Code Online (Sandbox Code Playgroud)
也就是说,该行是一个数组而不是哈希。有没有办法将其读取为哈希值,以便
row[:name]
row[:address]
Run Code Online (Sandbox Code Playgroud)
是数据。那是因为将来,如果表的列顺序不同,row[3]可能会变成row[5],所以row[:address]更加确定。
我有一个看起来像这样的邀请表
sqlite> .schema invitations
CREATE TABLE "invitations"
( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
, "sender_id" integer
, "recipient_email" varchar(255)
, "token" varchar(255)
, "sent_at" datetime
, "team_id" integer
, "created_at" datetime
, "updated_at" datetime
);
CREATE UNIQUE INDEX "index_invitations_on_recipient_email_and_team_id"
ON "invitations" ("recipient_email", "team_id");
CREATE INDEX "index_invitations_on_sender_id"
ON "invitations" ("sender_id");
CREATE INDEX "index_invitations_on_team_id"
ON "invitations" ("team_id");
Run Code Online (Sandbox Code Playgroud)
令牌列存储在记录创建时生成的hexdigests(Ruby):
self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
Run Code Online (Sandbox Code Playgroud)
当我将邀请插入数据库时,我可以使用它来检索它
SELECT * FROM "invitations" where "invitations"."recipient_email" = "an email"
Run Code Online (Sandbox Code Playgroud)
但
SELECT * FROM "invitations" where "invitations"."token" = …Run Code Online (Sandbox Code Playgroud) 我试图将SQLite数据库用于我的jruby项目.
当我试图安装sqlite3 gem时收到以下错误
C:\tibbr\main\community_manager>gem install sqlite3-ruby
Run Code Online (Sandbox Code Playgroud)
构建原生扩展.这可能需要一段时间...错误:安装sqlite3-ruby时出错:错误:无法构建gem本机扩展.
C:/tibbr/main/tools/jruby/bin/jruby.exe extconf.rb警告:JRuby不支持本机扩展,也不支持main:Object的Dir_config(NoMethodError)mkmf' library.
Check http://kenai.com/projects/jruby/pages/Home for alternatives.
extconf.rb:10: undefined method
Gem文件将保留在C:/tibbr/main/tools/jruby/lib/ruby/gems/1.8/gems/sqlite3-1.3.4中进行检查.结果记录到C:/tibbr/main/tools/jruby/lib/ruby/gems/1.8/gems/sqlite3-1.3.4/ext/sqlite3/gem_make.out
有什么建议?提前致谢
这是我的.CSV文件的示例:
column1,column2
data,moreData
foo,bar
cats,dogs
Run Code Online (Sandbox Code Playgroud)
使用Ruby和sqlite3 gem,有没有办法制作SQLite数据库并将.CSV数据导入其中?
我正在http://onemonth.com上关注我创建rails应用程序的教程.我被要求使用Heroku让我的应用程序在线直播.我按照通过SSH密钥将我的Github帐户链接到Heroku的步骤.但是,当我使用该命令heroku create然后复制过去提供的链接时,在这种情况下http://young-peak-7631.herokuapp.com/,我在浏览器中收到"应用程序错误".
这是确切的消息:
"应用程序中发生错误,无法提供您的页面.请稍后再试.如果您是应用程序所有者,请查看日志以获取详细信息."
请参阅下面的Heroku错误页面图片
这是我的日志:
2014-07-01T06:50:55.157229+00:00 app[web.1]: Rendered layouts/_header.html.erb (0.7ms)
2014-07-01T06:50:56.211672+00:00 heroku[router]: at=info method=GET path="/about" host=omr-photoshare.herokuapp.com request_id=0b8e9a43-5ffd-4bae-aa4f-61e9f87dfdfc fwd="65.78.4.236" dyno=web.1 connect=2ms service=192ms status=304 bytes=845
2014-07-01T06:50:56.025883+00:00 app[web.1]: Started GET "/about" for 65.78.4.236 at 2014-07-01 06:50:56 +0000
2014-07-01T06:50:56.082614+00:00 app[web.1]: Rendered pages/about.html.erb within layouts/application (0.2ms)
2014-07-01T06:50:56.174581+00:00 app[web.1]: Completed 200 OK in 95ms (Views: 7.0ms | ActiveRecord: 86.8ms)
2014-07-01T06:50:56.079586+00:00 app[web.1]: Processing by PagesController#about as HTML
2014-07-01T06:50:56.173874+00:00 app[web.1]: Rendered layouts/_header.html.erb (90.3ms)
2014-07-01T06:50:57.026362+00:00 heroku[router]: …Run Code Online (Sandbox Code Playgroud) CentOS 5.6上提供的最新SQLite3库已安装,版本为3.3.6.我的理解是sqlite3 gem需要3.6或更高版本.
如何在不升级服务器库的情况下让我的应用程序使用SQLite3?它在工作环境中,我根本不允许升级它.
我无法在OSX(Leopard)上运行以下ruby脚本.
require 'sqlite3'
database = SQLite3::Database.new( "new.database" )
database.execute( "create table sample_table (id INTEGER PRIMARY KEY, sample_text TEXT, sample_number NUMERIC);" )
database.execute( "insert into sample_table (sample_text,sample_number) values ('Sample Text1', 123)")
database.execute( "insert into sample_table (sample_text,sample_number) values ('Sample Text2', 456)")
rows = database.execute( "select * from sample_table" )
p rows
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
/Users/Ted/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- sqlite3/sqlite3_native (LoadError)
from /Users/Ted/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/Ted/.rvm/gems/ruby-1.9.2-p180/gems/sqlite3-1.3.4/lib/sqlite3.rb:6:in `rescue in <top (required)>'
from /Users/Ted/.rvm/gems/ruby-1.9.2-p180/gems/sqlite3-1.3.4/lib/sqlite3.rb:2:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:33:in `require'
from <internal:lib/rubygems/custom_require>:33:in `rescue in …Run Code Online (Sandbox Code Playgroud) 我得到了ruby 1.8.7(本机编译),rails 2.3.4,OSX 10.6.2以及sqlite3-ruby.
我访问rails应用程序时遇到的错误是
NameError:未初始化的常量SQLite3 :: Driver :: Native :: Driver :: API
历史:
我通过使用FW-cable从我的旧macbook迁移我的应用程序升级到雪豹.一切都运行了好几个月,但昨天我需要安装watir,这取决于rb-appscript,由于libsqlite3.dylib中的"错误的架构"错误而无法构建.我认为构建是在旧机器上进行的,所以我想重建sqlite3-ruby:
$ sudo gem uninstall sqlite3-ruby
$ sudo gem install sqlite3-ruby
构建原生扩展.这可能需要一段时间...
错误:安装sqlite3-ruby时
出错:错误:无法构建gem本机扩展./ usr/local/bin/ruby extconf.rb在-lrt中
检查fdatasync()...没有
检查sqlite3.h ...是
在-lsqlite3中检查sqlite3_open()... no
*extconf.rb失败*
由于某些原因无法创建Makefile,可能缺少
必要的库和/或标头.检查mkmf.log文件以获取更多
详细信息.您可能需要配置选项.
似乎sqlite3库不能正常工作.我已经尝试安装macports sqlite3(sudo port install sqlite3)并使用它,但结果相同...所以我从头开始重建sqlite3 .. download-> configure-> make-> make install.之后,gem现在构建完美,但在rails中不起作用,在本文的顶部给出了错误.
我不确定从哪里开始,因为我尝试了以下内容
我正在学习Ruby on Rails 3,并希望在Sqlite3中看到由各种Scaffold命令创建的db结构,以便更好地理解该过程.
在OSX Snow Leopard中,输入:
which sqlite3
收益率:
"在/ usr/local/bin目录/ sqlite3的"
但似乎无法找到数据库!指向该位置只会产生以下错误消息:
连接失败.文件已加密或不是数据库
使用以下方法查看dbs:
有人启发我dbs实际居住的地方吗?"/ usr/local/bin/sqlite3"似乎是一个符号链接:/usr/local/Cellar/sqlite/3.7.10/bin/sqlite3s
是否有一行简单的代码可以包含在种子文件的顶部,以便在插入新的种子数据之前清除每个表而不运行任何rake命令来回滚表或数据库?
我想的是:
[Foo, Bar].each{|class| class.destroy_all}
Run Code Online (Sandbox Code Playgroud)
关键是我想添加新数据,其中每个新插入从id开始:1.我想要避免的是删除一个包含100行的表,当我添加新数据时,它从101开始.
sqlite3-ruby ×10
sqlite ×6
ruby ×5
centos ×1
csv ×1
database ×1
heroku ×1
jrubyonrails ×1
postgresql ×1
rubygems ×1
sql ×1