Python Forum
how to validate user input from database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to validate user input from database
#1
Sad 
hi
i am creating a application i want to verify the user input name from my database.


These are my Two table

def create_tables(db):
    cur = db.cursor()

    cur.execute("""CREATE TABLE IF NOT EXISTS Habit (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    task_specification TEXT,
    period TEXT,
    created_at BOOLEAN,
    streak INT ) """)

    cur.execute("""CREATE TABLE IF NOT EXISTS count (
    name TEXT,
    created_at BOOLEAN,
    FOREIGN KEY (name) REFERENCES Habit(name))""")

    db.commit()
database function

def get_habit_data(db, name):
    cur = db.cursor()
    cur.execute("SELECT * FROM Habit WHERE name=?", (name,))
    result = cur.fetchone()
    if result is None:
        print("record already exists")
Where i test the application it prints "record already exists" when it not exist inthe database.
Gribouillis write Sep-10-2022, 05:31 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
According to Python's DB API, the method .fetchone() returns None when no more data is available in a query return set. So your code asks to print "record already exists" when the record does not exist.

The error is in your code's wrong logic.
Reply
#3
I never understood why people use IF NOT EXISTS.

In my opinion, you really should know what tables you have in your database. Somebody has to keep an eye on them!

Check in phpMyAdmin, or do this first:

Why would you go around randomly creating new tables? They are made for a purpose!

import pymysql

def showTables(): 
    # To connect remote MySQL database 
    conn = pymysql.connect( 
        host='123.456.789.123', 
        user='me',  
        password = 'secret', 
        db='mydb', 
        ) 
      
    cur = conn.cursor()

    sql = "SHOW TABLES"
    cur.execute(sql)
    output = cur.fetchall()
    mytables = []
    for i in output: 
        mytables.append(output[i][0])
      
    # To close the connection 
    conn.close()
    return mytables

existing_tables = showTables()

if not new_table in existing_tables:
    create_tables(db)
Reply
#4
(Sep-10-2022, 05:21 PM)johnconar Wrote:
def get_habit_data(db, name):
    cur = db.cursor()
    cur.execute("SELECT * FROM Habit WHERE name=?", (name,))
    result = cur.fetchone()
    if result is None:
        print("record already exists")

Your function is a bit strange, because it doesn't even return its result (it implicitly returns None in all cases). Also, testing by just running the application and printing stuff is not ideal. Why? Because we're humans and make mistakes - can you be certain that you've gotten the set up and inputs correct when you're testing? Not really, because there's nothing written down for you to look back at to see what you did. Also, you're going to need to check this stuff is still working as you change the code. Do you really want to repeat all these steps manually every time (which is time consuming and boring) and run the risk of not doing things correctly? Writing automated tests to get the computer to do the work solves these problems. Here are some example tests using the unittest module that's in Python's standard library:

import unittest

class TestHabitDatabase(unittest.TestCase):
    def test_when_no_habit_exists_with_the_given_name_None_is_returned(self):
        db = create_db() # Use production code to create the database!

        habit = get_habit_data(db, "doesnt-exist")

        self.assertEquals(habit, None)

    def test_a_habit_can_be_found_by_name(self):
        db = create_db()
        habit = Habit(
            id=None,
            name="Foo",
            task_specification="Bar",
            period="Baz",
            created_at=True,
            streak=0
        )
        add_habit(db, habit)

        retrieved_habit = get_habit_data(db, "Foo")

        self.assertEquals(retrieved_habit.name, "Foo")
        # Check other properties too


if __name__ == "__main__":
    unittest.main()
Saving the tests in a file called test_habit_database.py, you can run them with python3 test_habit_database.py (and they'll fail without the implementations of the various functions, obviously!).

Another thing to point out is that I've introduced a data class to model a habit:

from dataclasses import dataclass


@dataclass
class Habit:
    id: int
    name: str
    task_specification: str
    period: str
    created_at: bool # Really?
    streak: int
I'm assuming a habit is an important concept in your application, so it should be modelled so people know that when reading the code. Also, the database API returns a tuple and having to refer to indexes for the various fields in the calling code isn't very readable. So, as the test suggests, I'd have get_habit_data return a Habit object.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,074 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 926 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,076 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,106 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,108 Dec-11-2022, 07:39 PM
Last Post: snippsat
  validate large json file with millions of records in batches herobpv 3 1,283 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 958 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  How to split the input taken from user into a single character? mHosseinDS86 3 1,184 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Use pexpect to send user input alisha17 0 1,903 May-10-2022, 02:44 AM
Last Post: alisha17
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,496 Apr-05-2022, 06:18 AM
Last Post: C0D3R

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020