Author Topic: Python and databases  (Read 704 times)

0 Members and 1 Guest are viewing this topic.

Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
Python and databases
« on: May 22, 2013, 07:38:30 am »
Im writing this to kind of share my ideas and also welcome yours in this matter.
count me as a fresher here


Code: (Python) [Select]
import MySQLdb,time
#creating a separator
separator = "--------------------------------------------------------------------"
#connection defination
db= MySQLdb.connect(host="localhost", user="root", passwd="rooter",
db="test")
print separator
#warning
print ("&&&&&&&&&&&&&&&&&&&&&&&&&& date format yyyymmdd.&&&&&&&&&&&&&&&&&&&")
print separator
#Guest salutations


print "++++ Hello, welcome to Project manager by pytbyte pythons+++"
now = time.ctime()
print "it's now: ", now
print separator
print separator
#getting values from user
Guest = raw_input("Guest: ")
Project = raw_input("Project: ")
Partners = raw_input("Partners: ")
Goals = raw_input("Goals: ")
Budget = raw_input("Budget: ")
Begindate = int( raw_input("Date of begin: ") )
print separator


#allowing youser to view data being inserted
print "Guest: [" + Guest + "]"
print "Project: [" + Project + "]"
print "Partners: [" + Partners + "]"
print "Goals: [" + Goals + "]"
print "Budget: [" + Budget + "]"
print "Begindate: "+ str(Begindate)
print separator


#invoking cursor
cursor = db.cursor()


#preparing data to be inserted to database
stmt = "INSERT INTO trial (Guest, Project, Partners, Goals, Budget, Begindate) VALUES ('"


stmt = stmt + Guest
stmt = stmt + "', '"
stmt = stmt + Project
stmt = stmt + "', '"
stmt = stmt + Partners
stmt = stmt + "', '"
stmt = stmt + Goals
stmt = stmt + "', '"
stmt = stmt + Budget
stmt = stmt + "', "
stmt = stmt + str(Begindate)
stmt = stmt + ")"


#cursor performs the insertion
cursor.execute(stmt)
print ("                    Your project has been submited!")
print "  +++++++++ Goodbye,it's been nice working for you+++++++++"
print separator
print now
#stopping acctivities and closing database
cursor.close ()
db.commit ()
now another one
Code: (Python) [Select]

import MySQLdb


db= MySQLdb.connect(host="localhost", user="root", passwd="rooter",
db="testdb")
try:
    title = raw_input("Please enter a book title: ")
    if title == "" :
        exit
    author = raw_input("Please enter the author's name: ")
    pubdate = int( raw_input("Enter the publication year: ") )
except:
    print "Invalid value"
    exit


print "Title: [" + title + "]"
print "Author: ["+ author + "]"
print "Publication Date: " + str(pubdate)


cursor = db.cursor()


stmt = "INSERT INTO Books (BookName, BookAuthor, PublicationDate) VALUES ('"
stmt = stmt + title
stmt = stmt + "', '"
stmt = stmt + author
stmt = stmt + "', "
stmt = stmt + str(pubdate)
stmt = stmt + ")"
cursor.execute(stmt)
print "Record added!"


cursor.close ()
db.commit ()

« Last Edit: June 27, 2013, 07:31:04 pm by RedBullAddicted »
If you don't go into the tiger's cave, how will you get the cub?

Offline zoup

  • Serf
  • *
  • Posts: 29
  • Cookies: 3
  • I don't understand anything here !
    • View Profile
Re: Python and databases
« Reply #1 on: May 22, 2013, 11:56:53 am »
Nice one. I am still learning Python too.

Wouldn't it be better to format the print with % etc. ?






Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
Re: Python and databases
« Reply #2 on: May 23, 2013, 05:59:31 am »
im also kind of new in this but il read more on the same .i feel i use a lot of extra code.i guess i could do with a shorter script. :-[

If you don't go into the tiger's cave, how will you get the cub?