Comparing my input with values in my database - python

i recently got into programming and i'm having a hard time understanding how things work SO please help me. I created a database using mySQL and i filled it with some data (10 values called ID numbers),if i want the user to enter a value (his ID number) in my program (I'm using python) and i want my program to take this value and compare it with the values in my database then return "Already exists" if any of the values in my DB is equal to the value the user put, how do i do that ? and should i keep using python ? or is there anything that you would recommend to access/work on my databse ?
Thank you in advance,

You should first follow this mini-tutorial to connect Python with MySQL and be able to run MySQL commands in python.
Then it should be as simple as selecting all of the id's from the table and checking if any match with the user input. To select all of the ID's you'd use something like this (change it to match your database):
mycursor.execute("SELECT * FROM ID")
You can see a few more examples here.
I hope this helps.

Related

creating table in sql-server using python

I need to write a python program to create a table and add columns to it but the name and the quantity of tables and its columns will we user defined means I don't know the name and quantity, it will all be taken as an input from user.
any idea how to do it?
I think using the sqllite3 package it is possible. All you need to do is run the queries with a connection to your database.

Is there a way to return multiple values to python after a MySql query?

I am new to python and of course MySql. I recently created a Python function that generates a list of values that i want to insert to a table (2 columns) in MySql based on their specification.
Is it possible to create a procedure that can take a list of values that i'm sending through python, check if these values are already in one of my 2 two columns,
if they are already in the second one don't return,
if they are in the first one return all that are contained there
if they are in none of them return them with some kind of a flag so i can handle them through python and insert them to correct table
EXTRA EXPLANATION
Let me try to explain what i want to achieve so maybe you can give me a push and help me out. So, first i get a list of CPE items like this ("cpe:/a:apache:iotdb:0.9.0") in python and my goal is to save them into a database where the CPE's related to the IOT will be differentiated from the generic ones and saved in different tables or columns. My goal is that this distinction will be done by user input for each and every item but only once per item, so after parsing all items in python i want to first check in database if they exist in one of the tables or columns.
So for each and every list item that i pass i want to query mysql and:
if it exists in non iot column already don't return anything
if it exist in iot column already return item
if not exists anywhere return also item so i can get user input in python to verify if this is iot item or not and insert it to database after that
I think you could use library called pandas.
Idk if it is the best solution but it could work.
Export the thing you have in SQL into pandas or just query the SQL file using pandas.
Check out this library, it's really helpful for exploring data sets.
https://pandas.pydata.org/

SQLAlchemy: limit in the same string as where

We're trying to enable a SQL query front-end to our Web application, which is WSGI and uses Python, with SQLAlchemy (core, not ORM) to query a PostgreSQL database. We have several data layer functions set up to assist in query construction, and we are now trying to set something up that allows this type of query:
select id from <table_name> where ... limit ...
In the front end, we have a text box which lets the user type in the where clause and the limit clause, so that the data can be queried flexibly and dynamically from the front end, that is, we want to enable ad hoc querying. So, the only thing that we now firsthand is:
select id from <table_name>
And the user will type in, for example:
where date > <some_date>
where location is not null limit 10 order by location desc
using the same back end function. The select, column and table should be managed by the data layer (i.e. it knows what they are, and the user should not need to know that). However, I'm not aware of any way to get SQLAlchemy to automatically parse both the where clause and the limit clause automatically. What we have right now is a function which can return the table name and the name of the id column, and then use that to create a text query, which is passed to SQLAlchemy, as the input to a text() call.
Is there any way I can do this with SQLAlchemy, or some other library? Or is there a better pattern of which I should be aware, which does not involve parsing the SQL while still allowing this functionality from the front-end?
Thanks a lot! All suggestions will be greatly appreciated.
I'm not sure I follow, but the general SQL-Alchemy usage is like:
results = db.session.query(User).filter(User.name == "Bob").order_by(User.age.desc()).limit(10)
That will query the User table to return the top ten oldest members named "Bob"

preventing updation of specific columns in sqlite from python

i am developing a twisted app which interacts with a sqlite backend, in the sqlite db there is a users table of which certain columns should not be updated if they already contain a value.
one way of doing this would be to check the user table before each insert for existence of values in the columns of interest and proceed accordingly , but this will be a performance killer and people familiar with twisted will know how cumbersome this can be, can someone suggest a better way of doing this .
TIA
Try the INSERT OR IGNORE variation of the INSERT statement.

How to check if table in databse exist and how to display error?

How to check if the table in database exist before creating it by using Python?
I would like to check if it exist then drop the table. If not, then create..
Also, how to display error if there are some errors in the codes of database?
(If in PHP. Codes will be die("....."); for Error Handling.. So what should I use in Python?)
Any advice?
From the MySQL manal use
CREATE table IF NOT EXISTS ...
To create a table if it doesn't exist. That might even be standard SQL (not sure but it will work in sqlite too)!
As for your second question "how to display error if there are some errors in the codes of database?" not sure exactly what you mean. but if MySQL returns an error then the database module will turn it into a python exception - see this StackOverflow question for some discussion

Categories

Resources