Как создать подобную систему, которая обновляется как счетчик при нажатии кнопки с помощью таблицы SQL - PullRequest
0 голосов
/ 30 октября 2019

Я нигде не могу его найти. Я просто хочу помочь понять, как бы я поступил, это код, который я хотел бы вставить. Я считаю, что понадобится новая таблица. Ниже я говорю, куда он пойдет и откуда придет опция

import sqlite3, datetime
global username
conn = sqlite3.connect("test.db")

conn.execute('''CREATE TABLE IF NOT EXISTS Users
  (username PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL,
  time DATETIME NOT NULL,
  password INT NOT NULL); ''')

conn.execute('''CREATE TABLE IF NOT EXISTS posts
   (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
   username TEXT NOT NULL,
    yourpost TEXT NOT NULL,
    current DATETIME NOT NULL);''')


now = datetime.datetime.now()
current = now.strftime("%d-%m-%Y %H:%M:%S")
print(current)

def signUp():
    while True:
        username = input("Enter a username: ")
        if 0 < len(username) < 16:
            check = conn.execute("SELECT name FROM Users WHERE username = ?",(username,)).fetchone()
            if check == None:
                break
            else:
                print('username taken')
                menuOption()
    while True:
        name = input("Enter your full name: ")
        if 0 < len(name) < 16 and ' ' in name:
            break                   
    while True:
        email = input("enter your email: ")
        if '@' in email:
            break
    while True:
        password = input("Enter your password as long as its less than 8 characters: ")
        if len(password) < 8:
          break 
    conn.execute("INSERT INTO Users(username,name,email,time,password) VALUES(?,?,?,?,?)" , (username, name, email, current, password))
    conn.commit()

    print("your details have been saved\n")

    details = conn.execute('SELECT * FROM Users WHERE username = ?', (username,)).fetchall() 
    print('your info:', details)
    return username

def signIn():
    while True:
        usernameInput = input("Enter your username: ")
        check = conn.execute("SELECT name FROM Users WHERE username = ?",(usernameInput,)).fetchone()
        if check != None:
            break
        else:
            print('no user found by that name')
    while True:
        passwordInput = input('enter your password: ')
        check = conn.execute("SELECT name FROM Users WHERE password = ? AND username = ?",(passwordInput, usernameInput)).fetchone()
        if check != None:
            break
        else:
            print('incorrect password')

    details = conn.execute("SELECT * FROM Users WHERE username = ?",(usernameInput,)).fetchone()
    print('your info:', details)
    return usernameInput

def menuOption():
    print("Do you want to:\n1)Sign up\n2)Sign in")
    while True:
        userResponse = input(">>> ")
        if userResponse == '1' or userResponse == '2':
            break
        else:
            print('you must choose 1 or 2')
    if(userResponse == "1"):
        usr = signUp()
    elif(userResponse == "2"):
        usr = signIn()
    wpost(usr)

def get_all():
    print('\nall user info:\n')
    allusers = conn.execute('SELECT * FROM Users').fetchall()
    for i in allusers:
        print(i)

def post(username):
  while True:
      yourpost = input("This is your post, type whatever you want: ")
      conn.execute("INSERT INTO posts(username,yourpost,current) VALUES(?,?,?)" , (username, yourpost, current))
      conn.commit()
      allposts = conn.execute('SELECT * FROM Posts').fetchall()
      for x in allposts:
        print(x)
      edit_delete(username)


def Like_system(username)

, это где она будет

def edit(username):
  while True:
    editID = input("Enter the ID of the post you want to edit\n")
    youredit = input("edit your post here: ")
    update = conn.execute('UPDATE posts SET yourpost=? WHERE ID=?',(youredit, editID)).fetchone()
    conn.commit()
    yourpost(username)

def yourpost(username):
  print("Your posts are:\n")
  your_post = conn.execute("SELECT yourpost FROM posts WHERE username=?", (username,)).fetchall()
  for y in your_post:
    print(y)
def everyones_posts():
  print("the current posts are: \n")
  every_posts = conn.execute("SELECT username, yourpost FROM posts").fetchall()
  for z in every_posts:
    print(z)
def edit_delete(username):
    print("Do you want to: \n(E)edit a post\n(D)or delete a post \n(V)or view all posts \n(YV)or view your posts")
    while True:
      userResponse3 = input(">>> ")
      if userResponse3 == "E" or userResponse3 == 'D'or userResponse3 == 'V' or userResponse3 == 'YV':
        break
      else:
          print("you must choose E OR D OR V")
    if(userResponse3 == "E"):
        edit(username)
    if(userResponse3 == "D"):
        delete()
    if(userResponse3 == "V"):
        everyones_posts()
    if(userResponse3 == "V"):
        yourpost(username)


def delete(username):
  while True:
    yourpost(username)
    deleteID = input("enter the ID of the post you want to delete\n")
    yourdelete = conn.execute('Delete from posts WHERE ID=?',(deleteID)).fetchone
    conn.commit()




def yourpost(username):
  print("Your posts are:\n")
  your_post = conn.execute("SELECT yourpost FROM posts WHERE username=?", (username,)).fetchall()
  for y in your_post:
    print(y)
def everyones_posts():

  print("the current posts are: \n")
  every_posts = conn.execute("SELECT username, yourpost FROM posts").fetchall()
  for z in every_posts:
    print(z)
  like(username)
def edit_delete(username):
    print("Do you want to: \n(E)edit a post\n(D)or delete a post \n(V)or view all posts \n(P)or view your posts")
    while True:
      userResponse3 = input(">>> ")
      if userResponse3 == "E" or userResponse3 == 'D'or userResponse3 == 'V' or userResponse3 == 'P':
        break
      else:
          print("you must choose E OR D OR V OR P")
    if(userResponse3 == "E"):
        edit(username)
    if(userResponse3 == "D"):
        delete(username)
    if(userResponse3 == "V"):
        everyones_posts()
    if(userResponse3 == "P"):
        yourpost(username)


def wpost(username):
  print("Do you want to: \n(Y)create a post\n(N)not")
  while True:
    userResponse2 = input(">>> ")
    if userResponse2 == "Y" or userResponse2 == 'N':
      if(userResponse2 == "Y"):
        post(username)

    if(userResponse2 == "N"):
        get_all()
    else:
        print("you must choose Y OR N")
    if(userResponse2 == "Y"):
        post(username)

    if(userResponse2 == "N"):
        get_all()
        yourpost(username)
        edit_delete(username)

def like(username):
  print("Do you want to: \n(L)like a post\n(N)or not")
  while True:
    userResponse4 = input(">>> ")
    if userResponse4 == "L" or userResponse4 == 'N':
      if(userResponse4 == "L"):
        like_system(username)

    if(userResponse4 == "N"):
        get_all()
    else:
        print("you must choose L OR N")
    if(userResponse4 == "L"):
        Like_system(username)

    if(userResponse4 == "N"):
        get_all()
        yourpost(username)
        edit_delete(username)

и ссылка на выше

menuOption()
conn.close()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...