TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login CONVERT LENGTH
login CONVERT TEMPERATURE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login WORK CALCULATOR
login CALCULATE LIFE

Python 3 programming language, learn python, tutorial

Previous articlePage bottomNext article  ALL TOPICS

Python 3. Mysql databases. Examples

 Python MYSQL table operations:
https://www.tutorialspoint.com/python/python_database_access.htm
 
#!/usr/bin/python

import os
import time    
from time import sleep
import MySQLdb

lol=time.strftime('%Y-%m-%d')

os.system('color A')
os.system('cls')

g="0"
db = MySQLdb.connect("localhost","root","","users" )

while (g != "5"):
    if g == "0":
        os.system('cls')
        print("    MySQL")
        print("= = = = = = =")
        print("0. Meniu")
        print("1. Show All MySQL")
        print("2. INSERT")
        print("3. DELETE")
        print("4. UPDATE")
        print("5. Exit")
        print("= = = = = = =")
        g = input("Select: ")
    elif g == "1":
        os.system('cls')
        print("MySQL Table")
        print("= = = = = = =")
        
        cursor = db.cursor()
        cursor.execute("SELECT * FROM money")
        row = cursor.fetchone()
        for row in cursor:
            s = "ID: {0} | Name: {1} | Ammount: {2} | Date: {3}".format(row[0], row[1], row[2], row[3])
            print(s)
        
        g = input("Select: ")
    elif g == "2":
        os.system('cls')
        print(" INSERT")
        print("= = = = = = =")
        name=input("Name: ")
        am=input("Ammount: ")
        cursor = db.cursor()
        sql = "INSERT INTO money(name, ammount, date) VALUES ('%s', '%f', '%s')" % (name, float(am), lol)
        try:
           cursor.execute(sql)
           db.commit()
        except:
           db.rollback()
        print("= = = = = = =")
        g = input("Select: ")
    elif g == "3":
        os.system('cls')
        print(" DELETE")
        print("= = = = = = =")
        name=input("Name: ")
        cursor = db.cursor()
        sql = "DELETE FROM money WHERE name='%s'" % (name)
        try:
           cursor.execute(sql)
           db.commit()
           print("Success. Done.")
        except:
           print("Failed. Rolling back")
           sleep(1)
           db.rollback()
        print("= = = = = = =")
        g = input("Select: ")
    elif g == "4":
        os.system('cls')
        print(" Update")
        print("= = = = = = =")
        name=input("Name: ")
        am=input("Ammount: ")
        cursor = db.cursor()
        sql = "UPDATE money SET ammount='%f' WHERE name='%s'" % (float(am), name)
        try:
           cursor.execute(sql)
           db.commit()
           print("Success. Done.")
        except:
           print("Failed. Rolling back")
           sleep(1)
           db.rollback()
        print("= = = = = = =")
        g = input("Select: ")
    else:
        os.system('cls')
        print("Item not found")
        g = input("Select: ")    
os.system('cls')    
db.close()
print("    MySQL")
print("= = = = = = =")    
print("Exit..")
print("= = = = = = =")    
exit();
sleep(1)
os.system('python mysq.py')
 
 
Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
Both fortune and love befriend the bold.
Ovid
God buries His workmen but carries on His work.
Charles Wesley
Peace has its victories, but it takes brave men and women to win them.
Ralph Waldo Emerson