r/PythonLearning • u/Hidayat8811 • 5d ago
Showcase Database prototype (python)
I write my first data base code handling a class record using list tell me how is it..
First two are code and the rest is the output.
Tell me if anything can be improve or added to make this code even more practical.
410
Upvotes






2
u/Naive_Programmer_232 5d ago edited 5d ago
nice work. for your
remove_datafunction, look into.popmethod on dictionaries. if the name is in the dictionary, you can pop by key (it returns the value), and also updates. so to give example:you could wrap this in a try, because if something is requested that is not a key, it'll throw a
KeyError. So consider this other example:that way if it's wrapped you can handle the error without crashing the program and do whatever.
8
u/Traditional-Rub354 4d ago
Holy print statement from hell, why!
25
u/PureWasian 5d ago
Persistence. Your database is wiped as soon as you exit the runtime. A natural next progression would be writing the input/output
datainto a text/csv/json file per "session" running the script.Then you'll realize, it only saves all of the updates if things exit gracefully. You should update per db write transaction.
Then you'll realize, only one person can edit the db at a time. You should add a locking mechanism so you don't have concurrent writes.
At this point, you'll discover that it's probably a good time to look into sqlite for a more "proper" local, lightweight db layer. Use that to learn how to write queries (and note that you would need to update a lot of your calling code accordingly).
If you wanted to go further, branch out from sqlite to PostgreSQL or some other more beefy and feature-rich db layer, or look into how cloud hosting solutions work to see how people typically save, access, and modify persistent data across multiple users.
2
u/Electrical-Look8944 3d ago
Ai will replace u 😭
1
u/Hidayat8811 5d ago
I agree that it is not storing data any where it is just taking input and printing it . So as many of you said that I should do this in sqlite or in json, where do I start learning them from, what are the differences between them and which best for what. I will improve my code as much as I can .
4
u/zztopsboatswain 5d ago
This is a good prototype. Now look into SQLite or json to store the data, so you can save your changes
3
u/Sea-Ad7805 5d ago
See this quick demo of the sqlite3 database API. You store your data in tables, not in Python lists, and use SQL statements to query/modify your data.
3
u/Bmoneysign 4d ago
looking good! this is what a lot of computer science courses teach at first ! I’d recommend taking a look at Redis, SQL, And MongoDB for utilizing databases in your coding journey! Don’t let the other comments get you down. What you are working on is a great way to get experience writing code.
2
u/LegMysterious1899 5d ago
use json
2
u/No_Connection_2406 5d ago
It’s an interesting experiment but I’d use different tools. If you really want to write a database using python, start with Django.
2
u/PHASEONE10843 4d ago
Great!
Now, think about how you could make all of this persistent.
2
u/ChainsawLight 3d ago
Database prototyping seems cool
2
u/VenomSnakebigboss 2d ago
Woah dude thats crazy good did it work properly?
2
u/Ender_Locke 5d ago
this is data, in lists not in a database
1
u/_GreyPrinceZot3_ 5d ago
cool, but this is just data as it is. make a function that saves data in a file (use jason, yaml or even ur custom parsing if you wanna do it for fun, its not that hard) everytime soemthing gets chenged, or when you want. if you want, i can send you a lil useless project that i made a few months ago
1
u/Sidjeno 5d ago
use enums and named variable instead of magic variables.
Use an actual storage system, be it sqlite or json.
Tackle all the edge cases and make the system robust.
Develop a test suite to check for those edge cases.
1
u/RyanDubois43 5d ago
add a function to search records by name or ID, and use dicts instead of lists for faster lookups.
1
u/_DetectiveBrainz342 4d ago
Bro that is amazing
1
u/Congtuheo23 4d ago
so easy
1
u/Darthbyte_final_boss 4d ago
Yeah that's amazing 👏
1
u/Old-Confection-5129 3d ago
Each entity can be its own json file in a folder, and you can do all your normal crud stuff that way. That would be a good next step since you are working on the interface. Keep going!
1
u/garvrawlot 2d ago
love to see people writing code by hand.
1
u/PhoneOperatorIMHotel 2d ago
Glory to Cardassian union!
1
u/sleepbot63 5d ago
What's the goal may I ask ? coz I don't see a database here actually
0
u/badlinuxuzer 21h ago
Try to use the match/case construction sometime instead of if/elif
0
u/ArtynLuck 4d ago
That print statement cascade is terrifying. For starters stick your data in a JSON file before you accidentally summon a memory leak.