質問編集履歴
6
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
ruby上で実行してもだめだったので、sqlite3上でやってみたのですがそれでもだめでした
|
12
|
+
|
11
13
|
### 発生している問題・エラーメッセージ
|
12
14
|
|
13
15
|
|
5
タイトル変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
sqlite3,
|
1
|
+
ruby, dbi, sqlite3,テーブルデータのdelete
|
test
CHANGED
File without changes
|
4
文法修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -152,8 +152,6 @@
|
|
152
152
|
|
153
153
|
book_info.publish_date = Date.new( year, month, day )
|
154
154
|
|
155
|
-
#ここに大事な忘れ物があります(講義中に解説します)
|
156
|
-
|
157
155
|
@dbh.do("insert into bookinfos values
|
158
156
|
|
159
157
|
(
|
3
insert date型修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
-
insert into bookinfos values('aaa', 'bbb', 1, 2020
|
41
|
+
insert into bookinfos values('aaa', 'bbb', 1, '20200404');
|
42
42
|
|
43
43
|
|
44
44
|
|
2
hoge
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
-
insert into bookinfos values('aaa', 'bbb', 1, 2020
|
41
|
+
insert into bookinfos values('aaa', 'bbb', 1, 2020,4,4);
|
42
42
|
|
43
43
|
|
44
44
|
|
1
実際のコード全部載せました
test
CHANGED
File without changes
|
test
CHANGED
@@ -50,6 +50,224 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
+
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
|
57
|
+
require'date'
|
58
|
+
|
59
|
+
require'dbi'
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
class BookInfo
|
64
|
+
|
65
|
+
attr_accessor :title, :author, :pages, :publish_date
|
66
|
+
|
67
|
+
def initialize(title, author, pages, publish_date)
|
68
|
+
|
69
|
+
@title = title
|
70
|
+
|
71
|
+
@author = author
|
72
|
+
|
73
|
+
@pages = pages
|
74
|
+
|
75
|
+
@publish_date = publish_date
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def to_s
|
82
|
+
|
83
|
+
"#{@title}, #{@author}, #{@pages}, #{@publish_date}"
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
def toFormattedString(sep="\n")
|
90
|
+
|
91
|
+
"書籍名:#{@title}#{sep}著者名:#{@author}#{sep}ページ数:#{@pages.to_s}#{sep}発刊日:#{@publish_date.to_s}#{sep}"
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
class BookInfoManger
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
def initialize(db_fname = "book_infos.db")
|
104
|
+
|
105
|
+
@db_fname = db_fname
|
106
|
+
|
107
|
+
@dbh = DBI.connect("DBI:SQLite3:#{@db_fname}")
|
108
|
+
|
109
|
+
@dbh.do("create table if not exists bookinfos(
|
110
|
+
|
111
|
+
title varchar(50),
|
112
|
+
|
113
|
+
author varchar(50),
|
114
|
+
|
115
|
+
pages int,
|
116
|
+
|
117
|
+
publish_date date);")
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def addBookInfo
|
126
|
+
|
127
|
+
book_info = BookInfo.new("", "", 0, Date.new)
|
128
|
+
|
129
|
+
print "書籍名:"
|
130
|
+
|
131
|
+
book_info.title = gets.chomp
|
132
|
+
|
133
|
+
print "著者名:"
|
134
|
+
|
135
|
+
book_info.author = gets.chomp
|
136
|
+
|
137
|
+
print "ページ数:"
|
138
|
+
|
139
|
+
book_info.pages = gets.chomp.to_i
|
140
|
+
|
141
|
+
print "発刊年:"
|
142
|
+
|
143
|
+
year = gets.chomp.to_i
|
144
|
+
|
145
|
+
print "発刊月:"
|
146
|
+
|
147
|
+
month = gets.chomp.to_i
|
148
|
+
|
149
|
+
print "発刊日:"
|
150
|
+
|
151
|
+
day = gets.chomp.to_i
|
152
|
+
|
153
|
+
book_info.publish_date = Date.new( year, month, day )
|
154
|
+
|
155
|
+
#ここに大事な忘れ物があります(講義中に解説します)
|
156
|
+
|
157
|
+
@dbh.do("insert into bookinfos values
|
158
|
+
|
159
|
+
(
|
160
|
+
|
161
|
+
'#{book_info.title} ',
|
162
|
+
|
163
|
+
'#{book_info.author} ',
|
164
|
+
|
165
|
+
#{book_info.pages},
|
166
|
+
|
167
|
+
'#{book_info.publish_date} ');")
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
def listAllBookInfos
|
174
|
+
|
175
|
+
@dbh.select_all("select * from bookinfos") do |row|
|
176
|
+
|
177
|
+
puts BookInfo.new(row[:title], row[:author] ,
|
178
|
+
|
179
|
+
row[:pages] , row[:publish_date]).toFormattedString
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
def deleteBookInfos
|
188
|
+
|
189
|
+
print "削除する書籍名:"
|
190
|
+
|
191
|
+
d_title = gets.chomp
|
192
|
+
|
193
|
+
puts d_title.class
|
194
|
+
|
195
|
+
@dbh.do("delete from bookinfos where title = '#{d_title}';")
|
196
|
+
|
197
|
+
puts "削除完了"
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
def run
|
204
|
+
|
205
|
+
nums = []
|
206
|
+
|
207
|
+
while true
|
208
|
+
|
209
|
+
#メニュー表示(puts)
|
210
|
+
|
211
|
+
print "1.蔵書データの登録
|
212
|
+
|
213
|
+
2.蔵書データの表示
|
214
|
+
|
215
|
+
4.蔵書データの削除
|
216
|
+
|
217
|
+
9.終了
|
218
|
+
|
219
|
+
番号を選んでください(1,2,4,9):"
|
220
|
+
|
221
|
+
#メニュー番号受付(gets.chomp)
|
222
|
+
|
223
|
+
num = gets.chomp
|
224
|
+
|
225
|
+
#ログの保存
|
226
|
+
|
227
|
+
nums.push(num)
|
228
|
+
|
229
|
+
#メニュー番号に応じた処理を実行する(case / when)
|
230
|
+
|
231
|
+
case num
|
232
|
+
|
233
|
+
when "1"
|
234
|
+
|
235
|
+
#登録処理(addBookInfo)
|
236
|
+
|
237
|
+
addBookInfo
|
238
|
+
|
239
|
+
when "2"
|
240
|
+
|
241
|
+
#表示処理(listAllBookInfos)
|
242
|
+
|
243
|
+
listAllBookInfos
|
244
|
+
|
245
|
+
when "4"
|
246
|
+
|
247
|
+
deleteBookInfos
|
248
|
+
|
249
|
+
when "9"
|
250
|
+
|
251
|
+
break
|
252
|
+
|
253
|
+
#1番に戻る
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
bim = BookInfoManger.new
|
266
|
+
|
267
|
+
bim.run
|
268
|
+
|
269
|
+
```
|
270
|
+
|
53
271
|
### 試したこと
|
54
272
|
|
55
273
|
### 補足情報(FW/ツールのバージョンなど)
|