python3.5+mysql安装及编程学习

首先需要在官网下载MySQL,https://dev.mysql.com/downloads/installer/

在官网下载python3.5,https://www.python.org/downloads/release/python-354/

安装python3.5,安装MySQL,因为python3.5并没有官网的mysql接口,所以采用第三方库pymysql,pip install pymysql

采用以下例验证是否安装成功:

http://database.51cto.com/art/200511/12524.htm

设置主键,避免重复输入数据:https://www.cnblogs.com/haodawang/p/5967222.html,https://www.cnblogs.com/prayer21/p/6018864.html

python例程:http://www.runoob.com/mysql/mysql-drop-tables.html


之后可以编写python爬虫了,通过urllib,得到网页代码,http://blog.csdn.net/dangyang1992/article/details/53435016

教程有https://cuiqingcai.com/1052.html,本文目前简单的方法,即from urllib.request import urlopen和import re,urllib.request用于获取数据,re正则表达式提取数据。

比较好的爬虫是requests库https://cuiqingcai.com/2556.html,和Beautiful Soup库https://cuiqingcai.com/1319.html,

更高级是采用爬虫框架Scrapy。


# -*- coding: UTF-8 -*- 
import urllib.request  

page = urllib.request.urlopen(url)
html = page.read()
print (html) 


import pymysql
 
# 打开数据库连接
db = pymysql.connect("localhost","test","123456","TESTDB" )
 
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
 
#create database TESTDB;
#use TESTDB;
#create table EMPLOYEE( FIRST_NAME char(20), LAST_NAME char(20), AGE int(11), SEX char(1), INCOME float, primary key(FIRST_NAME) );
#DESCRIBE EMPLOYEE;


## SQL 删除语句
#sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (10)
#try:
#   # 执行SQL语句
#   cursor.execute(sql)
#   # 提交修改
#   db.commit()
#except:
#   # 发生错误时回滚
#   db.rollback()


# SQL 插入语句
sql = "INSERT IGNORE INTO EMPLOYEE(FIRST_NAME, \
       LAST_NAME, AGE, SEX, INCOME) \
       VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
       ('Mac1', 'Mohan', 20, 'M', 2000)
try:
   # 执行sql语句
   cursor.execute(sql)
   # 执行sql语句
   db.commit()
except:
   # 发生错误时回滚
   db.rollback()
   print("except")


# SQL 查询语句
sql = "SELECT * FROM EMPLOYEE \
       WHERE INCOME > '%d'" % (1000)
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 获取所有记录列表
   results = cursor.fetchall()
   for row in results:
      fname = row[0]
      lname = row[1]
      age = row[2]
      sex = row[3]
      income = row[4]
       # 打印结果
      print ("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
             (fname, lname, age, sex, income ))
except:
   print ("Error: unable to fetch data")
 

# 关闭数据库连接
db.close()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值