2016-12-01 7 views
0

私は投票アプリケーションを作成しています。ユーザーは各候補の間にカンマですべての候補を入力し、Enterキーを押して候補を投票します。sqliteが常に最初の値を挿入するループ

def newVote(event): 
       pos = position.get() 
       candidates = addCandEntry.get() 
       formatted = [x.strip() for x in candidates.split(',')] 
       for x in formatted: 
        c.execute("INSERT INTO current VALUES(?, ?, ?)", (pos,x,0,)) 
        c.execute('SELECT * FROM current WHERE position="Chief"') 
        print(c.fetchone()) 

私は位置dad,mom,son,sister,grandmaためのTkinterのエントリに次の値を渡すとしたらそれは

('Chief', 'dad', 0) 
('Chief', 'dad', 0) 
('Chief', 'dad', 0) 
('Chief', 'dad', 0) 
('Chief', 'dad', 0) 

がどのように私はそれだけで最初の代わりにそれぞれの値を追加することができますSQLでこの出力を与えるだろう1?

はここで、あなたが常に最初の行('Chief', 'dad', 0)を返しますfetchone()を使用しているので、あなたがそれ

import tkinter 
from tkinter import * 
import sqlite3 
import datetime 


# testing purposes only 
password = 'test' 

# create database file 
conn = sqlite3.connect('firefight') 
c = conn.cursor() 

# create tables 
c.execute('''CREATE TABLE IF NOT EXISTS users(
      username text, password text, admin boolean)''') 
c.execute('''CREATE TABLE IF NOT EXISTS positions(
      position text)''') 
c.execute('''CREATE TABLE IF NOT EXISTS current(
      position text, candidate text, votes int)''') 
c.execute('''CREATE TABLE IF NOT EXISTS past(
      date DATE, position text, candidate text, votes int, winner boolean)''') 

c.execute("INSERT INTO users VALUES('admin', 'VoteProgram', 'yes')") 


''' 
tables: 
users: 
    username text 
    password text 
    admin boolean 
positions: 
    position text 
current: 
    position text 
    candidate text 
    votes int 
past: 
    position text 
    candidate text 
    votes int 
    winner boolean 
''' 

# define root window 
root = tkinter.Tk() 
root.minsize(width=800, height = 600) 
root.maxsize(width=800, height = 600) 

# Admin sign in Label 
areAdmin = Label(root, text="Administrator sign in", font=("Arial", 18)) 
areAdmin.pack() 

# password label and password 
passwordLabel = Label(root, text="Password: ", font=("Arial", 12)) 
passwordLabel.place(x=300, y=30) 

# password entry 
adminPasswordEntry = Entry(root) 
adminPasswordEntry.place(x=385, y=32.5) 

# function for button 
def getEnteredPassword(event): 
    enteredPassword = adminPasswordEntry.get() 
    if enteredPassword == password: 
     # define admin window 
     admin = tkinter.Toplevel() 
     admin.minsize(width=800, height = 600) 
     admin.maxsize(width=800, height = 600) 
     # label saying to change password 
     changePasswordLabel = Label(admin, text="It is recommended to change your password the first time you log in.", 
            font=("Arial", 16), wraplength=500) 
     changePasswordLabel.pack() 
     # old password 
     changePassword_OldPasswordLabel = Label(admin, text="Old Password: ", font=("Arial", 12)) 
     changePassword_OldPasswordLabel.place(x=250, y=50) 
     changePassword_OldPassword = Entry(admin) 
     changePassword_OldPassword.place(x=365, y=52.5) 
     # new password 
     changePassword_NewPasswordLabel = Label(admin, text="New Password: ", font=("Arial", 12)) 
     changePassword_NewPasswordLabel.place(x=250, y=70) 
     changePassword_NewPassword = Entry(admin) 
     changePassword_NewPassword.place(x=365, y=72.5) 

     # function to change password 
     def passwordChangeCommand(event): 
      global password 
      oldPasswordValue = changePassword_OldPassword.get() 
      newPasswordValue = changePassword_NewPassword.get() 
      if oldPasswordValue == password: 
       password = newPasswordValue 
      else: 
       wrong = tkinter.Toplevel() 
       wrong.minsize(width=200, height = 100) 
       wrong.maxsize(width=200, height = 100) 
       Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, 
         fg="red").pack() 

     # submit button 
     newPasswordSubmit = Button(admin, text="Submit", width=10, command=passwordChangeCommand) 
     newPasswordSubmit.place(x=350, y=100) 

     newVoteLabel = Label(admin, text="Create New Vote", font=("Arial", 16)) 
     newVoteLabel.place(x=310, y=135) 

     # positions drop down 
     newVoteChoosePositionLabel = Label(admin, text="Choose the position you are voting for", font=("Arial", 12)) 
     newVoteChoosePositionLabel.place(x=265, y=170) 
     position = StringVar(admin) 
     position.set("Chief") 
     newVoteOption = OptionMenu(admin, position, "Chief", "First Lieutenant", "Second Lieutenant", "Third Lieutenant", 
            "Fourth Lieutenant") 
     newVoteOption.place(x=355, y=195) 

     #add candidates 
     addCandLabel = Label(admin, text="Add candidates below, separate each candidate with a comma then press enter when all candidates have been entered" 
          , font=("Arial", 11), wraplength=300, anchor=W) 
     addCandLabel.place(x=255, y=235) 
     addCandEntry = Entry(admin, width=40) 
     addCandEntry.place(x=272, y=295) 

     #new vote 
     newVoteButton = Button(admin, text="Create Vote", width=15) 
     newVoteButton.place(x=335, y=325) 

     def newVote(event): 
      pos = position.get() 
      candidates = addCandEntry.get() 
      formatted = [x.strip() for x in candidates.split(',')] 
      for x in formatted: 
       c.execute("INSERT INTO current VALUES(?, ?, ?)", (pos,x,0,)) 
       c.execute('SELECT * FROM current WHERE position="Chief"') 
       print(c.fetchone()) 


     addCandEntry.bind("<Return>", newVote) 
     newVoteButton.bind("<Button-1>", newVote) 

    else: 
     wrong = tkinter.Toplevel() 
     wrong.minsize(width=200, height=100) 
     wrong.maxsize(width=200, height=100) 
     Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, 
       fg="red").pack() 

# enter button for password 
passwordEnterButton = Button(root, text="Enter", width=10) 
passwordEnterButton.place(x=360, y=60) 
passwordEnterButton.bind("<Button-1>", getEnteredPassword) 
root.bind("<Return>", getEnteredPassword) 

mainloop() 
+0

私は非常に多くのpythonを知らないが、'addCandEntry.getは(何) "を返しますか? – gian1200

+0

は、挿入ループの前に 'formatted'を表示します。それは5回同じことのリストですか? –

+0

は、より良い文脈のための完全なコードを追加しました – Shniper

答えて

2

が必要な場合は、完全なコードです。しかし、フェッチに関係なく、のお父さん、お母さん、息子、姉妹、おばあちゃんの5つの別々のレコードがテーブルに追加されているはずです。あなたは、整数のプライマリを使用する場合に可能である各挿入後、最後の行を取得するためのもののようにしかし、それはそう

for x in formatted: 
    c.execute("INSERT INTO current VALUES(?, ?, ?)", (pos,x,0,)) 

for row in c.execute('SELECT * FROM current WHERE position="Chief"'): 
    print(row) 

:あなたは、各挿入をコミットして、別々のループで出力currentの各行を調整を考えてみましょうキー、各レコードに自動インクリメントid

あなたは、インサート列を一覧表示するには、強制的
c.execute('''CREATE TABLE IF NOT EXISTS users(
      id integer primary key, username text, password text, admin boolean)''') 
c.execute('''CREATE TABLE IF NOT EXISTS positions(
      id integer primary key, position text)''') 
c.execute('''CREATE TABLE IF NOT EXISTS current(
      id integer primary key, position text, candidate text, votes int)''') 
c.execute('''CREATE TABLE IF NOT EXISTS past(
      id integer primary key, date DATE, position text, candidate text, 
      votes int, winner boolean)''') 

c.execute("INSERT INTO users (username, password, admin)" + 
      " VALUES('admin', 'VoteProgram', 'yes')") 

その後、fetchone()を使用することができます:

for x in formatted: 
    c.execute("INSERT INTO current (position, candidate, votes) VALUES(?, ?, ?)", (pos,x,0,)) 
    c.execute('SELECT * FROM current WHERE id = (SELECT Max(sub.id) FROM current sub)') 
    print(c.fetchone()) 
関連する問題