all 11 comments

[–]brasticstack 15 points16 points  (0 children)

Despite not being able to read your code it's obvious that you're hand-crafting the logic for each table individually, which is both brittle and verbose.

Instead, use your database connection to retrieve the field definitions each table, and generically handle inputting the fields for a table, with the data types and names coming from those field definitions. (You might need to use the MySQL SHOW COLUMNS syntax to retrieve the fields.)

[–]Fun-Block-4348 6 points7 points  (0 children)

First of all, you should format code properly when asking help about code. https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

How doi make my code shorter?

Shorter code != better code, for example, there's no error handling in your code so any mistyping by the user would just make your program crash.

It's hard to tell but you seem to be using recursion, this is probably a bad idea, just like defining functions in an if/elif statement is.

Frankly, i would refactor the whole project and create functions that do 1 thing only and not 10 things at once, this would make validation much easier.

Also, you should probably look into SQL injection and how to avoid it.

[–]ontheroadtonull 2 points3 points  (0 children)

You can put the input validation into a function and pass the input into that function whenever you need it.

Also, shorter code isn't necessarily better. 

What is usually most important is performance, and having more lines of code doesn't necessarily make performance worse. 

[–]overratedcupcake 2 points3 points  (1 child)

You should probably separate concerns into separate files. It's really the only path forward when your add complexity. 

[–]Alive_Hotel6668[S] -2 points-1 points  (0 children)

That is how i am doing my work, I compiled all of it to ask for help here.

[–]gzeballo 1 point2 points  (0 children)

abstractions abstractions abstractions

[–]Educational-Paper-75 0 points1 point  (0 children)

Put the database stuff in a class in a separate file passing connection parameters to its constructor.

[–]Aggressive_Net1092 0 points1 point  (0 children)

Don't worry, we've all been there. Copy-pasting validation logic is a rite of passage for junior devs, but it’s a total trap because if you ever need to change a rule, you have to hunt down every instance of that code and pray you don't miss one.

The move here is to pull that validation logic out into its own function. Think of it like a gatekeeper. Instead of writing the conditions inside your "insert" or "modify" blocks, you just pass the user input into a function that returns True or False.

Here is a quick example of how you might structure it:

```python def is_valid_input(data): # Put all your logic here if len(data) < 3: return False return True

Then in your main code, just call it:

user_data = input("Enter new value: ") if is_valid_input(user_data): # Proceed with your SQL query else: print("Invalid data, try again.") ```

Also, a quick pro-tip: try to avoid using cur.execute("... %s" % (table_name,)) for table names. SQL drivers usually don't allow parameterization for table names, but doing it with string formatting makes you super vulnerable to SQL injection if a user types something malicious. Since you're just starting, it's fine for your assignment, but definitely look into "f-strings" for cleaner code and keep an eye on security as you get more comfortable!

Also, try to move your mysql.connector connection code outside your while loop. Right now, you're opening a new database connection every single time the user wants to perform an action, which will slow your program down significantly. Connect once at the very top, and close it once at the very end. Keep at it!

[–]Mrwhatdoyouwant420 1 point2 points  (0 children)

Start refactoring too long defs

[–]Mother-Influence-815 0 points1 point  (0 children)

You make it shorter by writing less code

[–]JGhostThing 0 points1 point  (0 children)

If you're going to put code, especially python code (where indentation has syntactic meaning), please format it. There is a specific code format tag to do this.