0

I want to store simple informations, like numbers in a mysql table using python. I linked my MySQL local server to PyCharm and I just don't know how to write the code in order to store the information in a table. I was thinking to use some simple "if's" and the printed result to be imediately stored in a certain table.

How can I do this?

Thanks! For starters, if I could store only the price, it would be great. Piece of my code:

    running = True

while running:
    try:
        buget = int(input("Money: "))
    except ValueError:
        print("Please enter your buget using only numbers!")
        continue
    else:
        print("Continue to the next step.")
        break

class Cars:
    def __init__(self,model,consumption,price,transmision):
        self.model = model
        self.consumption = consumption
        self.price = price
        self.transmision = transmision
        self.combination = model + " " + price

car_1 = Cars("BMW", "7%", "20000", ["Manual"])
car_2 = Cars("Audi", "8%", "30000 euro", ["Automated"])
car = (car_1, car_2)

for masini in car:
    guess = str(input("What car would you like? ").capitalize())
    if guess == "Bmw" and buget >= 20000:
        print("-----------------")
        print(car_1.model)
        print(car_1.consumption)
        print(car_1.price)
        print(",".join(car_1.transmision))
        print(car_1.combination)
    elif guess == "Audi" and buget >= 30000:
        print("-----------------")
        print(car_2.model)
        print(car_2.consumption)
        print(car_2.price)
        print(",".join(car_2.transmision))
        print(car_2.combination)
    elif guess == "Bmw" and buget < 20000:
        print("You can't afford this car.")
    elif guess == "Audi" and buget < 30000:
        print("You can't afford this car.")
    elif guess is not "Audi" and buget < 30000:
        print("This car doesn't exist!")
    elif guess is not "Bmw" and buget < 20000:
        print("This car doesn't exist.")
    else:
        print("This car is unavailable!")
    break
  • SO is not a replacement for the numerous online tutorials on relational databases and python db-api. – bruno desthuilliers Aug 01 '17 at 10:22
  • Have a look at pymysql library and do it with for loops similar to what you have with the print statements or put it into a pandas dataframe and run `pandas.to_sql` on it. – cardamom Aug 01 '17 at 10:29
  • Possible duplicate of [How do I connect to a MySQL Database in Python?](https://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python) – dben Aug 01 '17 at 13:57

1 Answers1

0

First, you need a driver which handles your connection. One which is wildly used is called: MySQLdb you can install it with pip or manually:

pip install MySQL-python

After you have the module you can connect to the db this way:

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

Make sure that you have imported the module at the top of your script with import MySQLdb. After this, you can start using the database. See a really good tutorial here.

Also have a look here as well.

dben
  • 484
  • 1
  • 6
  • 21