Python学习-pymysql(一)

Python学习-pymysql(一)

Python3.3如何连接MySQL数据库,并向数据库里面插入输入数据

例子:

#!/usr/bin/python
# -*- coding:utf-8 -*-
import pymysql

# 创建连接
conn = pymysql.connect(host='xxx', port=123, user='xxx', passwd='xxx', db='db', charset='utf8')
# 创建游标
cursor = conn.cursor()

# 执行SQL,并返回收影响行数
effect_row = cursor.execute("INSERT INTO `table1` (`destination_code`, `status`, `create_time`, `modify_time`, `date`, `transit_depot_no`, `slice_id`) VALUES ('123', NULL, now(), now(), now(), '755WF', '0')")


# 提交,不然无法保存新建或者修改的数据
conn.commit()

# 关闭游标
cursor.close()
# 关闭连接
conn.close()

原文地址:https://www.cnblogs.com/dmtz/p/8136971.html