Updating a Database,updatingdatabase,import MySQL
Updating a Database,updatingdatabase,import MySQL
import MySQLdbimport MySQLdb.cursorsdef get_column_name( data, prompt, names ) : value=-1 while value == -1: idx = 1 for col in data : print str(idx) + ': ' + col names.append( col ) idx = idx + 1 value = int( raw_input(prompt) ) if value < 1 or value >= idx : value = -1 return valueconn = MySQLdb.Connect( host='localhost', user='python-test', passwd='python', db='python-test')cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)cursor.execute('SELECT * FROM books')data = cursor.fetchone()names = []old_value = get_column_name( data, 'Which column do you want to change records for? ', names )names = []new_value = get_column_name( data, 'Which column do you want to change records to? ', names )old_val = raw_input('What value do you want to change for ' + names[old_value-1] + ': ')new_val = raw_input('What value do you want to change to for ' + names[new_value-1] + ': ')stmt = 'UPDATE books SET ' + names[new_value-1] + ' = ''+ new_val + '' WHERE ' + names[old_value-1] + ' = '' + old_val + '''print stmtcursor.execute(stmt)print 'Rows affected: ' + str(cursor.rowcount)cursor.close()conn.commit()conn.close()
- List Mini-Web Pages with Editing Links,mini-webediting
- pickle Module,picklemodule,#take almost
- unpickle the object,unpickleobject,#To unpickle
- Working with Directories,workingdirectories,# The os.p
- Working with File Objects,workingobjects,# Python has
- File Random Access,filerandomaccess,f = open(r'c
热门文章:
相关内容
- List Mini-Web Pages with Editing Links,mini-webediting, #!C:/Python
- pickle Module,picklemodule,#take almost
- unpickle the object,unpickleobject,#To unpickle
- Working with Directories,workingdirectories,# The os.pat
- Working with File Objects,workingobjects,# Python has
- File Random Access,filerandomaccess,f = open(r'c
- Adding Line Numbers to a Python Script,addingpython,import filei
- Writes the contents of string to the file,writescontents,f = open('
- Zip file,zipfile,import zipfi
- Demonstrates reading from a text file,,# print 'Ope
评论关闭