http://docs.sqlalchemy.org/en/latest/    sqlalchemy文档

1、下载,下载版本太多,不知道下哪个好,别人介绍版本

进入官网-->点击最下面 DOWNLOADS-->选择MySQL on Windows(installer & Tools)-->MySQL Installer-->选择

Windows (x86, 32-bit), MSI Installer 5.7.20 376.3M   (mysql-installer-community-5.7.20.0.msi)   我电脑是win10 64位,也不知道为什么选择32-bit

2、安装,正常一步一步点击安装

3、MYSQL数据库基本操作  python连接数据库要安装mysqlclient   pip install mysqlclient

4、mysql进入shell,win10 cortana 输入mysql shell 点击即可进入shell

python DB API: python 访问数据库的统一接口规范  https://www.python.org/dev/peps/pep-0249/

######  pymysql库的使用 #######
######  pymysql库的使用 #######
import pymysql
import pandas as pd #先做最基本的,再在实践中慢慢调整优化
#一、连接数据库
con=pymysql.connect(host='localhost',user='root',password='1***',database='world')
cursor=con.cursor() #二、创建数据库和表
#创建数据库
# create_schema="create schema abc"
# cursor.execute(create_schema)
# #创建表
# create_table='CREATE TABLE abc.Persons\
# (\
# Id_P int NOT NULL,\
# LastName varchar(255) NOT NULL,\
# FirstName varchar(255),\
# Address varchar(255),\
# City varchar(255)\
# )'
# cursor.execute(create_table)
#三、删除表、删除数据库
# drop_table="drop table abc.persons"
# cursor.execute(drop_table)
# drop_schema='drop schema abc'
# cursor.execute(drop_schema)
#四、写入数据
insert_data="insert into abc.Persons values(1,'a','b','c','d')"
cursor.execute(insert_data) #五、删除部分数据,如果某一行数据输入错误,可以删掉后再写入
#如果整个数据都要修改,可以删掉表,新建表
#1、删除一行(多行或多列可以用循环)
# drop_row='delete from abc.persons where Id_P=1'
# cursor.execute(drop_row)
#2、删除一列
drop_column='alter table abc.persons drop column City' # alter table ***.*** drop column **
cursor.execute(drop_column)
#3、添加一列或几列数据
# add_column='alter table abc.persons add City varchar(255)'
# cursor.execute(add_column)
con.commit()
#六、存入Dataframe数据  pd.DataFrame.to_sql
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
# http://docs.sqlalchemy.org/en/latest/core/engines.html
engine = create_engine("mysql+mysqldb://root:1234@localhost/ABC") #经测试,虽然没有import,但必须要安装MySQLdb模块,具体安装在下面
data=pd.DataFrame(np.arange(12).reshape(3,4),index=list('xyz'),columns=list('abcd'))
data.to_sql(name='personssss',con=engine, if_exists='append', index=False) #七、读取数据
#querystring
#querystring='SELECT * FROM sakila.actor'
#使用read_sql读取数据
#data=pd.read_sql(querystring, con);print(data.head())
###### MySQLdb库的使用 #######
资料来源:http://blog.csdn.net/heatdeath/article/details/65633896
###### MySQLdb库的使用 #######
#安装MySQLdb   pip install mysqlclient
#使用
import MySQLdb
#connect() 方法用于创建数据库的连接,里面可以指定参数:用户名,密码,主机等信息。
#这只是连接到了数据库,要想操作数据库需要创建游标。
conn=MySQLdb.connect(host='localhost', port=3306, user='root', passwd='1234', db='abc')
#通过获取到的数据库连接con下的cursor()方法来创建游标
cur=conn.cursor()
#创建数据表,通过游标cur 操作execute()方法可以写入纯sql语句。通过execute()方法中写如sql语句来对数据进行操作
#cur.execute('create table student(id int,name varchar(20),class varchar(30),age varchar(10))') #插入一条数据
#方法一
cur.execute("insert into student values('2','Tom','3 year 2 class','9')")
#方法二
sqli='insert into student values(%s,%s,%s,%s)'
cur.execute(sqli,('3','huhu','2 year 1 class','7')) #插入多条数据
cur.executemany(sqli,[
('3','Tom','1 year 1 class','6'),
('3','Jack','2 year 1 class','7'),
('3','Yaheng','2 year 2 class','7'),
])
#修改查询条件的数据
cur.execute("update student set class='3 year 1 class' where name='Tom'")
#cur.close() 关闭游标
cur.close()
#conn.commit()方法在提交事物,在向数据库插入一条数据时必须要有这个方法,否则数据不会被真正的插入。
conn.commit()
#con.close()关闭数据库连接
conn.close() #正常开发过程:
try:
  cursor.execute(sql_insert)
  print(cursor.rowcount)
except Exception as e:
  print(e)
  conn.rollback()

MYSQL安装与基本操作的更多相关文章

  1. mysql安装及基本操作(mysql作业)

    1 官网下载,链接  https://www.mysql.com/downloads/ Download MySQL Community Server 默认为你选好了Mac OS X 平台 选择的是. ...

  2. ubuntu linux mysql 安装 基本操作 命令

    mysql --help #如果有信息证明系统已经安装了mysql mysql -V #查看版本号 netstat -tap|grep mysql #检查mysql是否在启动状态 卸载mysql: s ...

  3. Day1 MySql安装和基本操作

    数据和数据库 1.数据:客观事物的符号表示. 2.存储介质:纸,光盘,磁盘,u盘,云盘… 3.存储的目的:检索(查询) 存储数据量加大,导致检索的难度升高. 4.数据库(DB:database):按照 ...

  4. MYSQL安装与库的基本操作

    mysql数据库 什么是数据库 # 用来存储数据的仓库 # 数据库可以在硬盘及内存中存储数据 数据库与文件存储数据区别 数据库本质也是通过文件来存储数据, 数据库的概念就是系统的管理存储数据的文件 数 ...

  5. 【mysql】centos7下mysql的安装以及基本操作

    centos7使用的MariaDB,替代了早期版本默认的MySQL.MariaDB是MySQL的一个分支,由开源社区维护,采用GPL授权许可,且MariaDB完全贱人MySQL. 检查centos7下 ...

  6. ubuntu下MySQL安装配置及基本操作

    在linux下安装方法: 分为四种:一: 直接用软件仓库自动安装(如:ubuntu下,sudo apt-get install mysql-server; Debain下用yum安装): 二:官网下载 ...

  7. MySql安装与使用(linux)

    安装 MySQL 注意:此处安装是yum安装为例: MySQL安装 #yum install mysql-server 完后显示如下: MySQL初始化 #service mysqld start 查 ...

  8. MySQL安装的挫折之路

    由于对MySQL卸载的不干净,mysql 的MySQL Connector Net/xxx无法卸载,后期重装无法成功.所以只能采用zip 安装https://www.cnblogs.com/Micha ...

  9. MySQL安装部署及调优

    MySQL安装 二进制安装 - mysql-5.5.49 mkdir /home/oldboy/tools -p cd /home/oldboy/tools/ rz #mysql-5.5.49-lin ...

随机推荐

  1. css 让多出的文字成省略号...

    一,单行 white-space:nowrap; overflow:hidden;text-overflow: ellipsis; 二,多行 display: -webkit-box; overflo ...

  2. 四篇关于chen_zhe的美文

    壹   chen_zhe人 那是谁 是谁 是谁 那就是 chen_zhe chen_zhe人 chen_zhe人 背负着暴政之名 抛弃了一切(指民心)而战斗(指禁言)的男人 chen_zhe代码是超音 ...

  3. phpstudy后门漏洞复现php5.2

    前段时间phpstudy被人发现某些版本存在后门,许多人就这样被当作肉鸡长达两年之久 后门隐藏在程序自带的php的php_xmlrpc.dll模块 影响的版本:phpstudy2016和2018 在H ...

  4. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'解决

    用XAMPP装装好mysql之后,mysql -uroot 连不上,报这个错误:   ERROR 2002 (HY000): Can't connect to local MySQL server t ...

  5. php二位数组排序(按子元素排序)

    array_multisort(array_column($mainTree[$note]["beCalls"], "wtp"), SORT_DESC, arr ...

  6. Eclipse 不能调试的问题

    现象 弹出 Cannot connect to VM Console 中的输出是: ERROR: transport error 202: connect failed: Connection ref ...

  7. php phar反序列化任意执行代码

    2018年 原理一.关于流包装stream wrapper大多数的文件操作允许使用各种URL协议去访问文件路径,如data://,zlib://,php://例如常见的有include('php:// ...

  8. MyBatis 逆向工程(MyBatis 自动生成接口以及xml)的使用

    刚学MyBatis逆向工程(还以为要反汇编呢.....) MyBatis逆向工程 个人理解就是链接数据库自动生成相关的增删改查相关的类 以及xml文件 (其中有一些不足 应该就是多表链接的问题需要自己 ...

  9. mpvue + Vant weapp + 微信云服务 打造小程序应用

    写在前面的话: 从事小程序开发已经大半年的时间了,但是一直都是再用原生写项目.一直想着用框架自己写一个小程序,但苦于一直没有时间.正好最近项目搁置,有了空闲时间,就研究了下mpvue + Vant w ...

  10. P1006 换个格式输出整数

    这道题相较于上一题来说就简单了许多.看题. 怎么感觉这道题有点类似P1002写出这个数.流程差不多,思路大致是先求出每一位上的数,然后根据 百十个 的顺序输出结果.题目比较简单,不做赘述,贴代码 代码 ...