首页 > 数据库技术 > 详细

python---连接MySQL第二页

时间:2015-11-05 11:58:36      阅读:278      评论:0      收藏:0      [点我收藏+]

用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)

 

python---连接MySQL第二页

原文:http://www.cnblogs.com/JiangLe/p/4938899.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!