اذهب إلى المحتوى
  • 0

كيفية تحقق الـ ID المستخدم مسبقا

Ran Om

السؤال

عندي مشكله بالكود ابي اذا احد كتب id مستخدم من قبل يطلع له تنبيه ان هذا ال id مستخدم من قبل وش الكود الي اكتبه بالضبط ؟

from pywebio.input import *

from pywebio.output import *

import sqlite3

mydb=sqlite3.connect("cvdb.db")

cr=mydb.cursor()

def create_db():

cr.execute('''CREATE TABLE if not exists cv ( id int, name varchar(255), age varchar(255), PRIMARY KEY (id) ) ''') create_db()

def select_user_type():

user_type=radio("Select the type of user",["New User","Old User"])

if user_type=="New User":

registration_form()

else:

search_user()

def registration_form():

data=input_group("Enter your data:",[

input("Enter your id:",name='id',type=NUMBER),

input("Enter your name:",name='name',type=TEXT), input("Enter your age:",name='age',type=NUMBER), ] ) cr.execute(f'''INSERT INTO cv(id, name, age) VALUES ('{data['id']}','{data['name']}','{data['age']}')''')

mydb.commit()

mydb.close()

put_table([ ["id", "Name", "Age"], [data['id'],data['name'], data['age']], ])

return data

def search_user():

id=input("Enter your id:",type=NUMBER)

command=f'''SELECT * FROM cv WHERE id='{id}'; ''' cr.execute(command) x=cr.fetchone() if x==None: popup("I can't find this id, please enter the correct id")

search_user()

else:

put_text("Your contact information ")

put_table([ ["id","Name", "Age"], [x[0],x[1],x[2]], ]) select_user_type()

app.py

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

عشان نتأكد ان الـ ID اللي المستخدم بيدخله مش موجود فعلا في الداتابيز، لازم نضيف جزء كود يتحقق من كده قبل ما نحاول ندخل سجل جديد:

from pywebio.input import *
from pywebio.output import *
import sqlite3

mydb = sqlite3.connect("cvdb.db")
cr = mydb.cursor()

def create_db():
    cr.execute('''CREATE TABLE if not exists cv (id int, name varchar(255), age varchar(255), PRIMARY KEY (id))''')
    mydb.commit()

create_db()

def select_user_type():
    user_type = radio("Select the type of user", ["New User", "Old User"])
    if user_type == "New User":
        registration_form()
    else:
        search_user()

def registration_form():
    data = input_group("Enter your data:", [
        input("Enter your id:", name='id', type=NUMBER),
        input("Enter your name:", name='name', type=TEXT),
        input("Enter your age:", name='age', type=NUMBER),
    ])
    # Check if the id already exists
    cr.execute("SELECT id FROM cv WHERE id = ?", (data['id'],))
    if cr.fetchone():
        popup("This ID is already used. Please use a different ID.")
        registration_form()  # Recall the form to enter the data again
    else:
        cr.execute("INSERT INTO cv (id, name, age) VALUES (?, ?, ?)", (data['id'], data['name'], data['age']))
        mydb.commit()
        put_table([["id", "Name", "Age"], [data['id'], data['name'], data['age']]])
        return data

def search_user():
    id = input("Enter your id:", type=NUMBER)
    cr.execute("SELECT * FROM cv WHERE id = ?", (id,))
    x = cr.fetchone()
    if x is None:
        popup("I can't find this id, please enter the correct id.")
        search_user()
    else:
        put_text("Your contact information")
        put_table([["id", "Name", "Age"], [x[0], x[1], x[2]]])
        select_user_type()

# Start the application
select_user_type()

هنا بعد ما المستخدم يدخل البيانات، بنعمل استعلام على الداتابيز عشان نشوف لو الـ ID ده موجود فعلا ولا لأ، باستخدام cr.fetchone(). 

لو الـ ID موجود فعلا، هنعرض رسالة تنبيه للمستخدم ونعيد استدعاء دالة registration_form() تاني عشان يقدر يدخل بيانات جديدة.

لو الـ ID مش موجود، هنا بس هندخل السجل الجديد في الداتابيز وهنعرض بياناته.

بكده بقينا متأكدين ان اي ID جديد هيتم ادخاله مش موجود فعلا قبل كده في الداتابيز.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...