美文网首页
python下mysql连接并使用

python下mysql连接并使用

作者: 不懒狮Blaise | 来源:发表于2017-10-13 18:08 被阅读0次

首先得安装支持python的mysql驱动,
目前有2个mysql驱动可以选择:
$ easy_install mysql-connector-python
$ easy_install MySQL-python
下面会使用MySQL-python驱动(包名很难记)

import MySQLdb
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
# 取连接的游标
cur = conn.cursor()

#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10)), PRIMARY KEY (id)")

#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")

#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")
#删除查询条件的数据
#cur.execute("delete from student where age='9'")

#关闭游标
cur.close()
# 连接提交操作
conn.commit()
#关闭连接
conn.close()

参考地址:http://www.cnblogs.com/fnng/p/3565912.html

相关文章

  • python下mysql连接并使用

    首先得安装支持python的mysql驱动,目前有2个mysql驱动可以选择:$ easy_install mys...

  • Python实现Mysql数据库连接池

    Python实现Mysql数据库连接池 python连接Mysql数据库: python编程中可以使用MySQLd...

  • python连接Mysql使用连接池

    python连接Mysql使用连接池 1、问题 当我们在Python中连接Mysql时,每次增、删、改、查如果都申...

  • 服务之路(一)

    连接mysql 使用koa框架,连接mysql,查询数据并通过接口返回

  • centos安装python3和mysql

    安装python 安装python 安装和使用虚拟环境 安装mysql mysql无法远程连接的问题: mysql...

  • Python Mysql

    使用python 连接mysql时,python开发环境需要配置,通常需要安装mysql的python库。 安装M...

  • python的sql操作

    需要使用pymysql包来连接mysql,使用python读取和写入数据到mysql中。

  • Node.js连接mysql数据库

    学习目标 使用node.js连接并操作mysql数据 先安装mysql模块 编写代码实现 创建连接 连接mysql...

  • Python3-mysql

    此文章记录一下python对mysql的一些操作方式、方法。 *python如何连接mysql ? python ...

  • python3 sqlalchemy No module nam

    Python ORM之一: sqlalchemy python3 使用sqlalchemy连接mysql数据库报错...

网友评论

      本文标题:python下mysql连接并使用

      本文链接:https://www.haomeiwen.com/subject/xbxtuxtx.html