Python 조작 mysql 첨삭 검사

2173 단어 Python자료.
#!C:/Python27  
#coding=utf-8

import MySQLdb

# MySQL    
mysql_host = '127.0.0.1'
mysql_user = 'root'
mysql_passwd = 'test'
mysql_port = '3306'
mysql_database = 'test'


def my_connect():
    """     """

    global conn, cursor
    
    #print MySQLdb.version_info
    
    try:
        conn = MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_passwd,charset='utf8', autocommit=True, db=mysql_database)
        
        print u"
Message: MySQL " except Exception: print(u"
Message: ") exit(20) cursor = conn.cursor() def add(): #add try: sql = "insert into user(name,age,insertTime) values(%s,%s,%s)" print sql param = ("tom",int(20),"2017-09-19 17:00:17") my_connect()# cursor.execute(sql,param) cursor.close() conn.close() except StandardError as e: print " 》》》》》",e,"《《《《《 " #conn.rollback() def update(): # my_connect() sql = "update user set name='%s' where id='%d'" %("ken",15) print sql try: cursor.execute(sql) cursor.close() conn.close() except StandardError as e: print " ",e def Select(): # try: n = cursor.execute("SELECT t.id,t.name,t.age,t.* from user t ORDER BY t.insertTime DESC LIMIT 10") data = cursor.fetchall() for row in data: # int str print ('id: ',row[0], ' name: ',row[1],' age ',row[2]) # cursor.close()# conn.close()# except Exception : # , conn.rollback() print(u"
, ") def delete(): # my_connect() sql = "delete from user where name='%s'"%("tom1") #parama =("tom") cursor.execute(sql) cursor.close() conn.close() my_connect() Select() add() update() delete()

좋은 웹페이지 즐겨찾기