質問編集履歴

1

文法・誤字・脱字の修正、エラーの明確化、

2018/04/28 08:09

投稿

Non-Face
Non-Face

スコア11

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,10 @@
5
5
  そこでSQLite文、DBについて質問があります。
6
6
 
7
7
  TkinterのEntryで会社名を"Google"と入力した場合Company_listという名前のテーブルにInsertし、同時に"Goggle"という名前のテーブルを作成したいのですが変数を使うとうまくいきません。
8
+
9
+ このエラーは二回目以降の実行によるものです。TkinterのEntryで入力した名前をcompanyに代入し、company(変数)という名前のテーブルを作成したいのですが変数ではないcompanyという名前のテーブルを作成してしまいます。変数をうまく使えていないことが原因だということはわかるのですが解決方法がわかりません。
10
+
11
+ SQL文のINSERTの中での変数の扱い方はご教示くださいました方法に変更しましたがCREATEの中での変数の扱いがわかりません。
8
12
 
9
13
  この解決方法がわかる方教えていただけると助かります。
10
14
 
@@ -16,7 +20,23 @@
16
20
 
17
21
  ```
18
22
 
19
- エラーメッセージ
23
+ 登録しました
24
+
25
+ Exception in Tkinter callback
26
+
27
+ Traceback (most recent call last):
28
+
29
+ File "/home/USERNAME/anaconda3/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
30
+
31
+ return self.func(*args)
32
+
33
+ File "teratail.py", line 34, in registration_name
34
+
35
+ Create table company(name TEXT, contents TEXT, memo TEXT)''')
36
+
37
+ sqlite3.OperationalError: table company already exists
38
+
39
+
20
40
 
21
41
  ```
22
42
 
@@ -36,9 +56,9 @@
36
56
 
37
57
  #データベース接続
38
58
 
39
- dbname="test.db"
59
+ dbname="teratail.db"
40
60
 
41
- conn.sqlite3.connect(dbname)
61
+ conn=sqlite3.connect(dbname)
42
62
 
43
63
  c=conn.cursor()
44
64
 
@@ -48,23 +68,23 @@
48
68
 
49
69
  try:
50
70
 
51
- ---c.execute('''Drop table if exists company_list''')
71
+ c.execute('''Drop table if exists company_list''')
52
72
 
53
- ---c.execute('''
73
+ c.execute('''
54
74
 
55
- ---Create table if not exists company_list
75
+ Create table if not exists company_list
56
76
 
57
- ---(company_name TEXT)''')
77
+ (company_name TEXT)''')
58
78
 
59
- ---c.execute("COMMIT;")
79
+
60
80
 
61
81
 
62
82
 
63
83
  except sqlite3.Error as e:
64
84
 
65
- ---print('sqlite3.Error occurred:', e.args[0])
85
+ print('sqlite3.Error occurred:', e.args[0])
66
86
 
67
- ---c.execute("COMMIT;")
87
+
68
88
 
69
89
 
70
90
 
@@ -72,23 +92,39 @@
72
92
 
73
93
  def registration_name():
74
94
 
75
- ---company_name=entry_company_name.get()
95
+ company_name=entry_company_name.get()
76
96
 
77
- ---try:
78
97
 
79
- ------c.execute('''
80
98
 
81
- ------Insert into company_list(company_name)
99
+ c.execute('''
82
100
 
83
- ------Values('{}');'''.format(company_name))
101
+ Insert into company_list(company_name)
84
102
 
85
- ------c.execute("COMMIT")
103
+ Values(?)''',(company_name,))
86
104
 
87
- ------print("登録しました")
105
+ c.execute("COMMIT")
88
106
 
89
- ---except:
107
+ print("登録しました")
90
108
 
109
+
110
+
91
- ------print("エラー")
111
+ company=entry_company_name.get()
112
+
113
+
114
+
115
+ c.execute('''
116
+
117
+ Create table company(name TEXT, contents TEXT, memo TEXT)''')
118
+
119
+ c.execute('''Insert into company Values('給料','○○万円','○○含む')''')
120
+
121
+ print(company)
122
+
123
+
124
+
125
+ conn.commit()
126
+
127
+
92
128
 
93
129
 
94
130
 
@@ -96,11 +132,11 @@
96
132
 
97
133
  root=Tk()
98
134
 
99
- win.geometry("600x600")
135
+ root.geometry("600x600")
100
136
 
101
137
 
102
138
 
103
- entry_company_name=Entry(root, font("",12))
139
+ entry_company_name=Entry(root, font=("",12))
104
140
 
105
141
  entry_company_name.pack()
106
142
 
@@ -118,34 +154,14 @@
118
154
 
119
155
  ### **試したこと**
120
156
 
121
- に def registration_name():のtry:の中に
157
+ ご指摘を元に書き換えまた。try:を一旦なくしエラーを表示させてみました。
122
158
 
123
- ```python
159
+ このエラーは二回目以降の実行によるものになります。
124
160
 
125
- ------try:
126
161
 
127
- ---------company=entry_company_name.get()
128
-
129
- ---------c.execute('''
130
-
131
- ---------Create table if not exists company(〜〜〜)'''
132
-
133
- ```
134
-
135
- と入れてみたのですが、SQL文内での変数の扱いが違うのかエラーがでました。
136
-
137
- 調べてみると、SQL文内での変数の扱いに関する情報がいくつかあるのですが全てInsertに関してでCreateについてはありませんでした。Tkinterのボタンのコマンドオプション先を2つ指定できればもうひとつdef文でもう一度SQL文のcreateを作れるのですがやはり変数の問題でできないです。
138
162
 
139
163
 
140
164
 
141
165
  ### 補足情報(FW/ツールのバージョンなど)
142
166
 
143
-
144
-
145
- 「---」読みやすように空白の代わりです。
167
+ 本ページ一度編集してす。
146
-
147
- 「〜〜〜」は今回の問題に干渉しないと思われますので省略しました。
148
-
149
-
150
-
151
- 質問の理解のしやすさやcodeの見やすさを考慮し極力短くしています。