yum install MySQL-python -y conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python',charset='utf8') The charset should be the same as the encoding of your database. If the database is gb2312, write charset='gb2312'. Then, this connection object also provides support for transactional operations, standard methods commit() commit rollback() rollback The method used by the cursor to execute the command: callproc(self, procname, args): used to execute the stored procedure, the received parameters are the stored procedure name and parameter list, and the return value is the number of rows affected execute(self, query, args): execute a single sql statement, the received parameters are the sql statement itself and the list of parameters used, and the return value is the number of rows affected executemany(self, query, args): executes the single-shot sql statement, but repeatedly executes the parameters in the parameter list, and the return value is the number of rows affected nextset(self): move to the next result set The method used by cursor to receive the return value: fetchall(self): Receive all returned result rows. fetchmany(self, size=None): Receive size pieces and return result rows. If the value of size is greater than the number of returned result rows, cursor.arraysize pieces of data will be returned. fetchone(self): Returns a result row. scroll(self, value, mode='relative'): Move the pointer to a row. If mode='relative', it means to move the value bar from the current row, if mode='absolute', it means to move from the first row of the result set Move the value bar one line. import MySQLdb try: conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='admin',db='mysql',port=3306) cur=conn.cursor() #cur.execute('select * from user') cur.execute('select version()') data = cur.fetchone () print "Databases version: %s " % data cur.close() conn.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1]) import MySQLdb as mdb import sys conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) with conn: cur = conn.cursor() cur.execute("Create TABLE IF NOT EXISTS \ Writers(Id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(25))") cur.execute("INSERT INTO Writers(Name) VALUES('Jack Luo')") cur.execute("INSERT INTO Writers(Name) VALUES('Kity Alice')") cur.execute("INSERT INTO Writers(Name) VALUES('Alex ok')") import MySQLdb as mdb import sys conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) with conn: cur = conn.cursor() cur.execute("SELECT * from Writers") rows = cur.fetchall() for row in rows: print row import MySQLdb as mdb import sys conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) with conn: cur = conn.cursor() cur.execute("SELECT * from Writers") numrows = int(cur.rowcount) for i in range(numrows): row = cur.fetchone() print row[0],row[1] #### import MySQLdb as mdb import sys conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) with conn: cur = conn.cursor(mdb.cursors.DictCursor) cur.execute("SELECT * FROM Writers") rows = cur.fetchall() for row in rows: print "%s %s" % (row["Id"],row["NAME"]) import MySQLdb as mdb import sys conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) with conn: cur = conn.cursor() cur.execute("SELECT * from Writers") rows = cur.fetchall() desc = cur.description print 'cur.description:',desc print "%s %3s" % (desc[0][0],desc[1][0]) for row in rows: print "%2s %3s" % row import MySQLdb as mdb import sys conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) with conn: cur = conn.cursor() cur.execute("UPDATE Writers SET NAME=%s WHERE Id = %s",("Guy de Maupasant","4")) print "Number of rows updated: %d" % cur.rowcount import MySQLdb as mdb import sys try: fin = open("logo.png") img = fin.read() fin.close() except IOError,e: print "Error %d: %s" % (e.args[0],e.args[1]) sys.exit(1) try: conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) cursor = conn.cursor() cursor.execute("INSERT INTO Images SET Data='%s'" % mdb.escape_string(img)) conn.commit() cursor.close() conn.close() except mdb.Error,e: print "Error %d:%s" % (e.args[0],e.args[1]) sys.exit(1) ### import MySQLdb as mdb import sys try: conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) cursor = conn.cursor() cursor.execute("SELECT Data FROM Images LIMIT 1") fout = open('logo2.png','wb') fout.write(cursor.fetchone()[0]) fout.close() cursor.close() conn.close() except IOError,e: print "Error %d: %s" %(e.args[0],e.args[1]) sys.exit(1) #### import MySQLdb as mdb import sys try: conn = mdb.connect(host='127.0.0.1',user='root',passwd='admin',db='game',port=3306) cursor = conn.cursor() cursor.execte("UPDATE Writers SET Name = %s WHERE Id = %s",("JACK LUO","1")) cursor.execte("UPDATE Writers Set Name = %s where Id = %s ",("Alex LI",'2')) cursor.execte("UPDATE Writers Set Name = %s where Id = %s",("Leonid Leonov",'3')) conn.commit() cursor.close() conn.close() except mdb.Error,e: conn.rollback() print "Error %d: %s" % (e.args[0],e.args[1])