python连接mysql操作(1)

python连接mysql操作(1)

import pymysql
import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host=‘10.10.146.28‘,
    port=3306,
    user=‘admin_m‘,
    passwd=‘fcfmTbRw1tz2x5L5GvjJ‘,
    db=‘test‘,
    charset=‘utf8mb4‘
)

def create_table():
    cursor = connect.cursor()

    # 使用execute()方法执行SQL,如果表存在就删除
    cursor.execute("drop table if exists employee")

    # 使用预处理器语句创建表
    sql ="""CREATE TABLE employee(
    id int not null auto_increment,
    first_name varchar(20) not null,
    last_name varchar(20) not null,
    age int not null default 0,
    sex int not null default ‘0‘,
    income decimal not null default 0.00,
    create_time datetime,
    primary key(id)
    ) Engine=InnoDB DEFAULT CHARSET=utf8mb4 comment="员工表"
    """

    try:
        cursor.execute(sql)
        print("CREATE TABLE employee success.")
    except Exception as e:
        print("CREATE TABLE employee failed, CASE:%s" % e)
    finally:
        cursor.close()
        # 关闭数据库连接
def main():
    create_table()

if __name__ == "__main__":
    main()

原文地址:https://www.cnblogs.com/bjx2020/p/11806063.html

时间: 2024-08-23 23:22:17

python连接mysql操作(1)的相关文章

[python] 连接MySQL操作

环境:Linux CentOS6.7,python 2.7.13 说明:连接MySQL,进行增删改查操作,并将执行的SQL和耗时记录到日志里 demo: #!/usr/bin/env python # -*- coding:utf-8 -*- import MySQLdb import logging import time ''' 设置日志输出路径和格式 ''' logging.basicConfig(level=logging.INFO,                     format

Python 连接 Mysql 操作异常

UserWarning: /home/toddler/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable). warnings.

Python连接Mysql数据库(Debian)

Python连接Mysql数据库(Debian) 以下是Python 2.*版本的安装方法,MySQL-python暂不支持Python 3.*版本 提前要做的工作: 安装setuptools,在终端中运行 wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python 安装pip,下载“get_pip.py”,运行 python get_pip.py 运行如下命令装好必要的包 sudo apt-get install python-d

python 连接mysql多层结构实例

一.python 连接mysql多层结构: 目录文件介绍: sqlexec             sql执行操作,例:增删改查 table                 数据库表模块 一个表一个模块 index.py            主文件 conf.py              数据库连接信息 目录结构如下: -rw-r--r-- 1 root root     167 Jun 22 20:36 conf.py-rw-r--r-- 1 root root     244 Jun 2

Python连接MySQL数据库之pymysql模块使用

Python连接MySQL数据库之pymysql模块使用 Python3连接MySQL PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. Django中也可以使用PyMySQL连接MySQL数据库. PyMySQL安装 pip install pymysql 连接数据库 注意事项 在进行本文以下内容之前需要注意: 你有一个MySQL数据库,并且已经启动. 你有可以连接该数据库的用户名和密码 你有一个有权限操作的datab

寒假学习进度-6(Python连接MySQL数据库)

Python连接mysql和操作 软件:pycharm 开始在pycharm下面的Terminal中安装mysql时提醒pip版本不够,所以需要先升级一下pip python -m pip install --upgrade pip 升级完pip之后就可以下载mysql pip install mysql 下载完成后在setting中查看 进行代码测试 #!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb db =MySQLdb.con

python连接mysql数据库——版本问题

今天终于解决了使用python连接数据库不成功的问题,现将过程总结如下: 一.出现的问题 在使用python连接mysql数据库是一直出现如下问题: 1.只能连接到我数据库中的的第一个数据库,但是不能操作里面的表,会报错表不存在.(表是存在的)2.更换其他数据库后,直接报错找不到该数据库.(数据库和表均存在) 运行连接数据库的代码,会出现: conn = pymysql.connect(user='root', password='password', database='XXX') Trace

Python连接MySQL的实例代码

Python连接MySQL的实例代码 MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/ 下载解压缩后放到%Python_HOME%/Lib/site-packages目录中,python会自动找到此包. MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0. 其他: 1. 平台及版本 linux 内核2.6,gcc 3.4.4,glibc 2

[笔记]--在Ubuntu系统用Python连接Mysql数据库

环境:Ubuntu11.10,Python2.7,Mysql5.0.95 在Ubuntu终端输入命令安装Python的Mysql模块 sudo apt-get install python-mysqldb 就这么简单: 运行一下脚本: #!/usr/bin/python #-*-coding=utf-8# # import MySQLdb cn = MySQLdb.Connection(host="192.168.88.124",user="root",passwd