python---连接MySQL第二页
用python向MySQL插入值、并取出被插入行的主键。
#!/usr/bin/python #!coding:utf-8 import mysql.connector from mysql.connector import errorcode def insert_data(**kwargs): config={ 'host':kwargs.get('host','127.0.0.1'), 'port':kwargs.get('port',3306), 'user':kwargs.get('user','admin'), 'password':kwargs.get('password','131417') } cnx=None cursor=None rowid=None try: cnx = mysql.connector.connect(**config) cursor=cnx.cursor() cursor.execute('insert into studio.t(x) values(2)') rowid=cursor.lastrowid cnx.commit() except mysql.connector.Error,err: print err finally: if cursor is not None: cursor.close() cnx.close() print 'the db resource has been release ...' if __name__=='__main__': insert_data(host='127.0.0.1')