データベース作成
インサート
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"database / sql" </span> <span class="s">"log"</span> <span class="n">_ </span> <span class="s">"github.com/mattn/go-sqLite3" </span> <span class="p">)</span> <span class="k">var </span> <span class="n">DbConnection </span> <span class="o">* </span><span class="n">sql </span><span class="o">。</span><span class="n">DB</span> <span class="k">func </span> <span class="n">main </span><span class="p">()</span> <span class="p">{ </span> <span class="n">DbConnection </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">sql </span><span class="o">。</span><span class="p">(</span><span class="s">"sqlite3" </span><span class="p">、</span><span class="s">"./example.sql" </span><span class="p">)を</span><span class="n">開き</span><span class="p">、 </span><span class="s">DbConnectionを</span><span class="k">延期し</span><span class="n">ます</span><span class="o">。</span><span class="n">閉じる</span><span class="p">()</span> <span class="c">// </span> <span class="s">テーブル</span><span class="c">作成</span><span class="n">cmd </span> <span class="o">:= </span> <span class="s">`存在しない場合はテーブルを</span><span class="c">作成します</span><span class="s">person( 名前STRING、 age INT) ` </span> <span class="n">_ </span><span class="p">、</span> <span class="n">err </span> <span class="o">:= </span> <span class="n">DbConnection </span><span class="o">。</span><span class="n">Exec </span><span class="p">(</span><span class="n">cmd </span><span class="p">)</span> <span class="c">// </span> <span class="n">エラー</span><span class="c">ハンドリング</span><span class="k">if </span> <span class="n">err </span> <span class="o">!= </span> <span class="no">nil </span> <span class="p">{ </span> <span class="n">log </span><span class="o">。</span><span class="n">Fatalln </span><span class="p">(</span><span class="n">err </span><span class="p">)</span> <span class="p">}</span> <span class="c">//レコードINSERT </span> <span class="n">cmd </span> <span class="o">= </span> <span class="s">"INSERT INTO person(name、age)VALUES(?、?)" </span> <span class="n">_ </span><span class="p">、</span> <span class="n">err </span> <span class="o">= </span> <span class="n">DbConnection </span><span class="o">。</span><span class="n">Exec </span><span class="p">(</span><span class="n">cmd </span><span class="p">、</span> <span class="s">"Nancy" </span><span class="p">、</span> <span class="m">20 </span><span class="p">)</span> <span class="k">if </span> <span class="n">err </span> <span class="o">!= </span> <span class="no">nil </span> <span class="p">{ </span> <span class="n">log </span><span class="o">。</span><span class="n">Fatalln </span><span class="p">(</span><span class="n">エラー</span><span class="p">)</span> <span class="p">} </span> <span class="p">}</span> |
1 2 3 4 5 6 7 8 |
PS C:\ Users \ yuta \ go \ src \ semaphore> sqlite3。\ example.sql SQLiteバージョン3.32.3 2020-06-18 14:00:33 使い方のヒントは「.help」と入力してください。 sqlite> .table 人 sqlite> sqlite> select * from person; ナンシー| 20 |
選択する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"database / sql" </span> <span class="s">"fmt" </span> <span class="s">"log"</span> <span class="n">_ </span> <span class="s">"github.com/mattn/go-sqLite3" </span> <span class="p">)</span> <span class="k">var </span> <span class="n">DbConnection </span> <span class="o">* </span><span class="n">sql </span><span class="o">。</span><span class="n">DB</span> <span class="k">タイプ</span> <span class="n">Person </span> <span class="k">構造体</span> <span class="p">{ </span> <span class="n">名前</span> <span class="kt">文字列</span> <span class="n">Age </span> <span class="kt">int </span> <span class="p">}</span> <span class="k">func </span> <span class="n">main </span><span class="p">()</span> <span class="p">{ </span> <span class="n">DbConnection </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">sql </span><span class="o">。</span><span class="p">(</span><span class="s">"sqlite3" </span><span class="p">、</span><span class="s">"./example.sql" </span><span class="p">)を</span><span class="n">開き</span><span class="p">、 </span><span class="s">DbConnectionを</span><span class="k">延期し</span><span class="n">ます</span><span class="o">。</span><span class="n">閉じる</span><span class="p">()</span> <span class="c">// </span> <span class="s">テーブル</span><span class="c">作成</span><span class="n">cmd </span> <span class="o">:= </span> <span class="s">`存在しない場合はテーブルを</span><span class="c">作成します</span><span class="s">person( 名前STRING、 age INT) ` </span> <span class="n">_ </span><span class="p">、</span> <span class="n">err </span> <span class="o">:= </span> <span class="n">DbConnection </span><span class="o">。</span><span class="n">Exec </span><span class="p">(</span><span class="n">cmd </span><span class="p">)</span> <span class="c">// </span> <span class="n">エラー</span><span class="c">ハンドリング</span><span class="k">if </span> <span class="n">err </span> <span class="o">!= </span> <span class="no">nil </span> <span class="p">{ </span> <span class="n">log </span><span class="o">。</span><span class="n">Fatalln </span><span class="p">(</span><span class="n">err </span><span class="p">)</span> <span class="p">}</span> <span class="c">//レコード挿入</span> <span class="n">cmd </span> <span class="o">= </span> <span class="s">"SELECT * FROM person where age =?" </span> <span class="n">行</span> <span class="o">:= </span> <span class="n">DbConnection </span><span class="o">。</span><span class="n">QueryRow </span><span class="p">(</span><span class="n">cmd </span><span class="p">、</span> <span class="m">20 </span><span class="p">)</span> <span class="k">var </span> <span class="n">p </span> <span class="n">Person </span> <span class="n">err </span> <span class="o">= </span> <span class="n">row </span><span class="o">。</span><span class="n">スキャン</span><span class="p">(</span><span class="o">&</span><span class="n">P </span><span class="o">。</span><span class="n">名前</span><span class="p">、</span> <span class="o">&</span><span class="n">P </span><span class="o">。</span><span class="n">年齢</span><span class="p">)</span> <span class="k">の場合</span> <span class="n">、ERR </span> <span class="o">!= </span> <span class="no">nilの</span> <span class="p">{ </span> <span class="k">場合</span> <span class="n">ERR </span> <span class="o">== </span> <span class="n">SQL </span><span class="o">。</span><span class="n">ErrNoRows </span> <span class="p">{ </span> <span class="n">ログ</span><span class="o">。</span><span class="n">Println </span><span class="p">(</span><span class="s">"行なし" </span><span class="p">)</span> <span class="p">}</span> <span class="k">さもなければ</span> <span class="p">{ </span> <span class="n">ログ</span><span class="o">。</span><span class="n">Println </span><span class="p">(</span><span class="n">err </span><span class="p">)</span> <span class="p">} </span> <span class="p">} </span> <span class="n">fmt </span><span class="o">。</span><span class="n">println </span><span class="p">(</span><span class="n">P </span><span class="o">。</span><span class="n">名</span><span class="p">、</span> <span class="n">P </span><span class="o">。</span><span class="n">年齢</span><span class="p">)</span> <span class="p">}</span> |
ナンシー20
`
削除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"database / sql" </span> <span class="s">"log"</span> <span class="n">_ </span> <span class="s">"github.com/mattn/go-sqLite3" </span> <span class="p">)</span> <span class="k">var </span> <span class="n">DbConnection </span> <span class="o">* </span><span class="n">sql </span><span class="o">。</span><span class="n">DB</span> <span class="k">タイプ</span> <span class="n">Person </span> <span class="k">構造体</span> <span class="p">{ </span> <span class="n">名前</span> <span class="kt">文字列</span> <span class="n">Age </span> <span class="kt">int </span> <span class="p">}</span> <span class="k">func </span> <span class="n">main </span><span class="p">()</span> <span class="p">{ </span> <span class="n">DbConnection </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">sql </span><span class="o">。</span><span class="p">(</span><span class="s">"sqlite3" </span><span class="p">、</span><span class="s">"./example.sql" </span><span class="p">)を</span><span class="n">開き</span><span class="p">、 </span><span class="s">DbConnectionを</span><span class="k">延期し</span><span class="n">ます</span><span class="o">。</span><span class="n">閉じる</span><span class="p">()</span> <span class="c">// </span> <span class="s">テーブル</span><span class="c">作成</span><span class="n">cmd </span> <span class="o">:= </span> <span class="s">`存在しない場合はテーブルを</span><span class="c">作成します</span><span class="s">person( 名前STRING、 age INT) ` </span> <span class="n">_ </span><span class="p">、</span> <span class="n">err </span> <span class="o">:= </span> <span class="n">DbConnection </span><span class="o">。</span><span class="n">Exec </span><span class="p">(</span><span class="n">cmd </span><span class="p">)</span> <span class="c">// </span> <span class="n">エラー</span><span class="c">ハンドリング</span><span class="k">if </span> <span class="n">err </span> <span class="o">!= </span> <span class="no">nil </span> <span class="p">{ </span> <span class="n">log </span><span class="o">。</span><span class="n">Fatalln </span><span class="p">(</span><span class="n">err </span><span class="p">)</span> <span class="p">}</span> <span class="c">//レコードDELETE </span> <span class="n">cmd </span> <span class="o">= </span> <span class="s">"DELETE FROM person WHERE name =?" </span> <span class="n">_ </span><span class="p">、</span> <span class="n">err </span> <span class="o">= </span> <span class="n">DbConnection </span><span class="o">。</span><span class="n">Exec </span><span class="p">(</span><span class="n">cmd </span><span class="p">、</span> <span class="s">"Nancy" </span><span class="p">)</span> <span class="k">if if </span> <span class="n">err </span> <span class="o">!= </span> <span class="no">nil </span> <span class="p">{ </span> <span class="n">log </span><span class="o">。</span><span class="n">Fatalln </span><span class="p">(</span><span class="n">エラー</span><span class="p">)</span> <span class="p">} </span> <span class="p">}</span> |
教材
https://www.udemy.com/course/go-fintech/learn/lecture/12088980