回答編集履歴

1

具体的な操作!

2016/10/24 06:42

投稿

nagaetty
nagaetty

スコア1106

test CHANGED
@@ -11,3 +11,75 @@
11
11
  データベースに値を設定する前(一番最初に)create文でデータの配置(table:Excelのシートのような物)を定義する必要があります。
12
12
 
13
13
  色々なデータベースの共通処理です。(sqlite,mysql,Postgersqlなどなど)
14
+
15
+
16
+
17
+ ```python
18
+
19
+ # -*- coding: utf-8 -*-
20
+
21
+
22
+
23
+ import sqlite3
24
+
25
+
26
+
27
+ connector = sqlite3.connect("sqlite_test.db")
28
+
29
+ c = connector.cursor()
30
+
31
+ # executeメソッドでSQL文を実行する
32
+
33
+ create_table = '''create table test_table (id int, name varchar(64))'''
34
+
35
+ c.execute(create_table)
36
+
37
+ sql = "insert into test_table values('1', 'python')"
38
+
39
+ connector.execute(sql)
40
+
41
+ sql = "insert into test_table values('2', 'パイソン')"
42
+
43
+ connector.execute(sql)
44
+
45
+ sql = "insert into test_table values('3', 'ぱいそん')"
46
+
47
+ connector.execute(sql)
48
+
49
+
50
+
51
+ connector.commit()
52
+
53
+ connector.close()
54
+
55
+ ```
56
+
57
+
58
+
59
+ 内容の確認
60
+
61
+ ```
62
+
63
+ # sqlite3 sqlite_test.db
64
+
65
+ SQLite version 3.7.17 2013-05-20 00:56:22
66
+
67
+ Enter ".help" for instructions
68
+
69
+ Enter SQL statements terminated with a ";"
70
+
71
+ sqlite> .tables
72
+
73
+ test_table
74
+
75
+ sqlite> select * from test_table ;
76
+
77
+ 1|python
78
+
79
+ 2|パイソン
80
+
81
+ 3|ぱいそん
82
+
83
+ sqlite>.quit
84
+
85
+ ```