- blogs:
- cles::blog
2010/01/18
[ by hsur at 18:44]
SQLiteをインメモリで使う
SQLiteはデータのちょっとした加工や管理に便利なので、最近よく使っているのですが、ファイルの指定を:memory:とすると、インメモリで使うことができるという機能をみつけたので、Rubyからちょっと使ってみました。
For example:
rc = sqlite3_open(":memory:", &db);
When this is done, no disk file is opened. Instead, a new database is created purely in memory. The database ceases to exist as soon as the database connection is closed. Every :memory: database is distinct from every other. So, opening two database connections each with the filename ":memory:" will create two independent in-memory databases.
† 利用例
#!/usr/bin/ruby
require 'rubygems'
require 'sqlite3'
db = SQLite3::Database.new(':memory:')
db.execute(<<EOS)
CREATE TABLE test (
id varchar(255),
PRIMARY KEY(id)
)
EOS
stmt = db.prepare(<<EOS)
INSERT INTO test(id) VALUES(:id)
EOS
stmt.execute( :id => "a" )
stmt.execute( :id => "b" )
stmt.execute( :id => "c" )
stmt.execute( :id => "d" )
stmt.close
count = db.get_first_value("SELECT count(*) FROM test")
puts "count(*): #{count}"
db.execute('select * from test') do |row|
puts row.join("\t") + "\n"
end
db.close
require 'rubygems'
require 'sqlite3'
db = SQLite3::Database.new(':memory:')
db.execute(<<EOS)
CREATE TABLE test (
id varchar(255),
PRIMARY KEY(id)
)
EOS
stmt = db.prepare(<<EOS)
INSERT INTO test(id) VALUES(:id)
EOS
stmt.execute( :id => "a" )
stmt.execute( :id => "b" )
stmt.execute( :id => "c" )
stmt.execute( :id => "d" )
stmt.close
count = db.get_first_value("SELECT count(*) FROM test")
puts "count(*): #{count}"
db.execute('select * from test') do |row|
puts row.join("\t") + "\n"
end
db.close
ファイルに書き出されないという部分以外は普通にSQLiteですね。
上記スクリプトの実行結果は下記のようになります。
count(*): 4
a
b
c
d
a
b
c
d
このエントリは役に立ちましたか?
トラックバックについて
Trackback URL:
お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
このエントリへのTrackbackにはこのURLが必要です→http://blog.cles.jp/item/3380
Trackbacks
このエントリにトラックバックはありません
Comments
愛のあるツッコミをお気軽にどうぞ。[policy]
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
コメントはありません
Comments Form
OpenID を使ってログインすることができます。
Copyright © 2004-2010 by CLES All Rights Reserved.
sp-20100320024235644595552@cles.net
sp-20100320024235644595552@cles.net
サイト内検索
おしらせ
検索ワード
- sqlite メモリ
- Yahoo! - 10/03/19 01:01:12
- sqlite :memory:
- Yahoo! - 10/03/17 20:11:26
- SQLite DISTINCT COUNT
- Yahoo! - 10/03/17 15:49:41
- sqlite db.execute ruby query
- bing - 10/03/11 00:50:41
- SQLite "select count"
- Yahoo! - 10/03/08 22:14:15
- sqlite インメモリー
- bing - 10/03/03 17:21:39
- ruby sqlite 検索
- Yahoo! - 10/03/02 12:35:36
- sqlite count
- Yahoo! - 10/02/24 14:36:47
- sqlite SELECT COUNT(*)
- Google - 10/02/22 11:12:58
- sqlite 高速化
- Yahoo! - 10/02/19 11:27:16
Tags [All Tags]
google | php | ruby | windows | firefox | linux | SoftwareEngineering | java | spam | ranking | curry | camera | seo | FireEmblem | thunderbird | server | marketing | mobile | Doctoral | docomo
へぇが多いエントリ [Top 100]
- おめでとうございます (4)
- 知恵の輪 サターン編 (3)
- SourceForge.JPのSubversion... (3)
- 人生初の出来事 (3)
- サーバセットアップ (3)
- ブックマークボタンを1つに (3)
- 和食 小錦 (3)
- 散髪しました (3)
- .inはインドのccTLDなのか (3)
- やっと髪をきりました (3)
閲覧数が多いエントリ [Top 100]
1 . ドラゴンクエストVIの影響力 [8972x]
2 . やっぱりあった!パクれる読書感想文! [7636x]
3 . Echofon for Firefox [6394x]
4 . OpenIDで自分のサイトのURLを使う [5757x]
5 . 急性胃腸炎 [5755x]
2 . やっぱりあった!パクれる読書感想文! [7636x]
3 . Echofon for Firefox [6394x]
4 . OpenIDで自分のサイトのURLを使う [5757x]
5 . 急性胃腸炎 [5755x]
最新のエントリ [archives]
最新のコメント [Latest 100]
- CD-ROM起動で、HDDを完全消去
- NP_Moblog v1.16
- pinzoro 01/15
- hsur 12/29
- and more...
- 耳がおかしいと思ったら突発..
- baca 01/13
- hsur 01/13
- and more...
カテゴリ別エントリ
cles::blogについて
Syndicate
Calendar
Referrers
Blog People
Admin
★はてな認証APIをつかってログインすることができます。
Powered by NP_Paint




