新浦京娱乐场官网Writing to a Database

2019-06-19 20:02栏目:技术创新

import MySQLdb

db= MySQLdb.connect(host='localhost', user='python-test', passwd='python',
db='python-test')
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 ()

版权声明:本文由新浦京娱乐站_新浦京娱乐场官网_新浦京娱乐发布于技术创新,转载请注明出处:新浦京娱乐场官网Writing to a Database