• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python 专题九 Mysql数据库编程基础知识

python 专题九 Mysql数据库编程基础知识

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python mysql数据库,python3连接mysql,python mysql,python怎么连接mysql,python连接mysql等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在Python网络爬虫中,通常是通过TXT纯文本方式存储,其实也是可以存储在数据库中的;同时在WAMP(Windows、Apache、MySQL、PHP或Python)开发网站中,也可以通过Python构建网页的,所以这篇文章主要讲述Python调用MySQL数据库相关编程知识。从以下几个方面进行讲解:

1.配置MySLQ
2.SQL语句基础知识
3.Python操作MySQL基础知识
4.Python调用MySQL示例

一. 配置MySQL

        
        首先下载mysql-5.0.96-winx64,安装过程如下图所示。
        1.安装MySQL 5.0
 

 

        2.选择手动配置、服务类型、通用多功能型和安装路径
 

 

        3.设置数据库访问量连接数为15、端口为3306(代码中设置URL用到)、编码方式为utf-8
 

 

        4.设置默认超级root用户的用户名和密码,最后安装成功
 

二. SQL语句基础知识


        安装MySQL 5.0成功后,进行数据库的简单操作。
        1.运行MySQL输入默认用户密码123456



         2.创建数据库test01和使用数据库(第二次调用直接use database)
         create database test01;



        显示数据库中包含的数据库:show databases;


         3.创建表student,其中学号为主键
         create table student(username varchar(20),password varchar(20),stuid int primary key);



        4.显示表结构,使用语句desc student



        5.向学生表中插入数据并显示查询的数据



        6.删除表:drop table student;


        7.更新数据
        update student set password='000000' where stuid='1';

        8.删除数据
        Delete from student where username='eastmount;

        此时MySQL操作数据库基本讲解结束,你同样可以实现数据库的增删改查、事务、存储过程等操作,建议安装可视化的软件来替代黑框,或使用Navicat for MySQL软件即可。代码如下:

Enter password: ******
mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mysql       |
| performance_schema |
| test        |
| test01       |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test01;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table student(username varchar(20),
  ->         password varchar(20),
  ->         stuid int primary key);
Query OK, 0 rows affected (0.33 sec)

mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| student     |
+------------------+
1 row in set (0.00 sec)

mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | YES |   | NULL  |    |
| password | varchar(20) | YES |   | NULL  |    |
| stuid  | int(11)   | NO  | PRI | NULL  |    |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.03 sec)

mysql> insert student(username, password, stuid)
  -> values('eastmount','123456',1)
  -> ;
Query OK, 1 row affected (0.05 sec)

mysql> select * from student;
+-----------+----------+-------+
| username | password | stuid |
+-----------+----------+-------+
| eastmount | 123456  |   1 |
+-----------+----------+-------+
1 row in set (0.00 sec)

mysql> update student set password='000000' where stuid='1';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student;
+-----------+----------+-------+
| username | password | stuid |
+-----------+----------+-------+
| eastmount | 000000  |   1 |
+-----------+----------+-------+
1 row in set (0.00 sec)

mysql> delete from student where username='eastmount';
Query OK, 1 row affected (0.08 sec)

mysql> select * from student;
Empty set (0.00 sec)

mysql>
</div>

三. Python调用MySQL基础知识

通常的安装方法是使用:pip install mysql 安装Python的MySQL库,但是总会报错。常见错误如:
Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat)
mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
这些可能是驱动等问题。

正确安装方法:
建议下载一个MySQL-python-1.2.3.win-amd64-py2.7.exe文件进行安装。
官网地址:https://pypi.python.org/pypi/MySQL-python/
下载地址:http://www.jb51.net/softs/73369.html

下面我们要详细了解Python数据库API。从Python中访问数据库需要接口程序,接口程序是一个Python模块,它提供数据库客户端库(通常是C语言写成的)的接口供你访问。注意:Python接口程序都一定要遵守Python DB-API规范。
DB-API是一个规范。它定义了一系列必须的对象和数据库存取方式,以便为各种各样的底层数据库系统和多种多样的数据库接口程序提供一致的访问接口。DB-API为不同的数据库提供了一致的访问接口,在不同的数据库之间移植代码成为一件轻松的事情。

下面简单介绍DB-API的使用方法。

1.模块属性
DB-API规范里的以下特性和属性必须提供。一个DB-API兼容模块定义如下所示:

apilevel:模块兼容的DB-API版本号
threadsafety:线程安全级别
paramstyle:支持sql语句参数风格
connect():连接数据库
</div>

Python调用MsSQL需要导入MySQLdb库,如下:

import MySQLdb

 2.connec

您可能想查找下面的文章:

  • python 专题九 Mysql数据库编程基础知识
  • Python如何读取MySQL数据库表数据
  • python操作mysql数据库
  • Python连接mysql数据库的正确姿势
  • Python操作MySQL数据库9个实用实例
  • Python操作MySQL数据库9个实用实例
  • python连接MySQL数据库实例分析
  • python中使用mysql数据库详细介绍
  • python操作MySQL数据库具体方法
  • python操作MySQL数据库具体方法

相关文章

  • Python读取Excel的方法实例分析
  • Python中IPYTHON入门实例
  • 在tensorflow中设置保存checkpoint的最大数量实例
  • Python日期操作学习笔记
  • 解读Python selenium的等待方式
  • python不带重复的全排列代码
  • Python实现简单状态框架的方法
  • Python Tkinter基础控件用法
  • Python运行的17个时新手常见错误小结
  • 简单谈谈Python中函数的可变参数

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python使用CMD模块更优雅的运行脚本
    • Python中的sort()方法使用基础教程
    • 使用 Python 获取 Linux 系统信息的代码
    • Python的Django框架使用入门指引
    • python 计算文件的md5值实例
    • 讲解python参数和作用域的使用
    • Python实现脚本锁功能(同时只能执行一个脚本)
    • python实现应用程序在右键菜单中添加打开方式功能
    • python paramiko实现ssh远程访问的方法
    • Python 的内置字符串方法小结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有