python3 pymysql模块

备注:用python 语句 操作mysql;仅供自己参考

环境:win10 x64 Python3.5.3


例子:

# 导入MySQL驱动:
>>> import pymysql
# 注意把password设为你的root口令:
>>> conn = mysql.connector.connect(user='root', password='password', database='test')
>>> c = conn.cursor()
# 创建user表:
>>> c.execute('create table user (id varchar(20) primary key, name varchar(20))')
# 插入一行记录,注意MySQL的占位符是%s:
>>> c.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael'])
>>> c.rowcount
1
# 提交事务:
>>> conn.commit()
>>> c.close()


# 运行查询:
>>> c = conn.cursor()
>>> c.execute('select * from user where id = %s', ('1',))
>>> values = cursor.fetchall()
>>> values
[('1', 'Michael')]
# 关闭Cursor和Connection:
>>> cursor.close()
True
>>> conn.close()


# 删除表:
>>> c = conn.cursor()
>>> c.execute('drop table user’)
# 关闭Cursor和Connection:
>>> cursor.close()
True
>>> conn.close()

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页