python安装mysql数据库和实现增删查改操作

xiaoxiao2021-02-27  199

主要记录一下在ubuntu 16.04下安装mysql 数据库,和Python MySQL开发的环境安装以及常见的mysql指令。 python版本是2.7

环境安装

安装mysql:

sudo apt-get update sudo apt-get install mysql-server #安装mysql服务器 sudo apt install mysql-client sudo apt install libmysqlclient-dev

安装过程会让输入密码,跟着步骤来就行。 测试是否安装成功,出现下面数据则成功。

xuna@xuna-virtual-machine:~$ sudo netstat -tap | grep mysql tcp 0 0 localhost:mysql *:* LISTEN 1068/mysqld

启动mysql

sudo service mysql start

安装python库的MySQLdb模块

sudo apt-get install python-pip sudo apt-get install python-dev sudo pip install mysql-python

测试下模块是否安装成功,新建一个test.py,编写如下代码:

import MySQLdb print MySQLdb

运行之后,打印出来MySQLdb模块的信息则安装成功:

xuna@xuna-virtual-machine:~/桌面$ python test.py <module 'MySQLdb' from '/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.pyc'>

创建数据库和表结构

为了下面演示python操作数据库,需要创建一个数据库和一个表(这是我用的都是在命令行下执行的),同时也记录mysql数据几个常见的命令 进入数据库

mysql -u root -p

root是创建mysql时的用户名,执行输入命令,进入数据库

xuna@xuna-virtual-machine:~/桌面$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

下面我们看一下mysql现在有哪些数据库,初次显示只有前四个,test数据库是我自己建立的。

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.01 sec)

创建数据库test1:

mysql> create database test1; Query OK, 1 row affected (0.00 sec)

查看创建的数据库(多出来一行我们刚才创建的数据库)

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | test1 | +--------------------+ 6 rows in set (0.00 sec)

创建数据库表,首先我们要进入数据库使用use命令

mysql> use test1; Database changed

查看test1数据库中的表(因为我还没有创建表,所以显示的是empty)

mysql> show tables; Empty set (0.00 sec)

现在创建一个名为account的表,有字段id和name两项,且字段id为主键

mysql> create table account( -> id int(4) primary key auto_increment, -> name char(20) not null); Query OK, 0 rows affected (0.03 sec)

现在查看一下数据库表(可以看到刚刚创建的数据库)

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

也可以使用下面的命令看表的结构

mysql> describe account; +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | | NULL | | +-------+----------+------+-----+---------+----------------+ 2 rows in set (0.03 sec)

现在向表里添加几组数据

mysql> insert into account values ( '1','name1'); Query OK, 1 row affected (0.02 sec)

使用selec查看添加的数据

mysql> select * from account; +----+-------+ | id | name | +----+-------+ | 1 | name1 | | 2 | name1 | | 3 | name3 | | 4 | name4 | | 5 | name5 | | 6 | name6 | +----+-------+ 6 rows in set (0.00 sec)

Python操作数据库(重点)

这里主要通过一个小例子演示一下,注释很详细,就不多说啦。

介绍 #connection对象支持下面4个方法 #cursor() 游标对象用于执行查询和获取结果,它支持下面六个方法 # execute(op[,args]) 执行一个数据库查询和命令 # fetchone 取的结果集的下一行 # fetchmany (size) 获取结果集的下几行 # fetchall() 获取剩余的全部 # rowcount 最近一次执行execute返回数据的行数或影响行数 # close() 关闭游标对象 #commit() 提交当前事务 #rollback() 回滚当前事务 #close() 关闭连接

链接代码

# -*- coding:utf-8 -*- import MySQLdb """ 建立连接 """ conn = MySQLdb.Connect( host = '127.0.0.1', port = 3306, #注意端口号为数字类型,其余都为字符串 user = 'root', passwd = 'root', db = 'test1', charset = 'utf8' ) cursor = conn.cursor() """ 查询操作 """ sql = "select * from account" cursor.execute(sql) print cursor.rowcount #单行查询 rs = cursor.fetchone() print rs #多行查询 rs = cursor.fetchmany(2) print rs #全部查询 rs = cursor.fetchall() print rs """ 插入操作 """ sql_insert = "insert into account values(%d,%s)" cursor.execute(sql_insert,(7,name7)) print cursor.rowcount """ 更新操作 """ sql_update = "update account set name = 'xuna' where id =4" cursor.execute(sql_update) print cursor.rowcount """ 删除操作 """ sql_delete = "delete from account where id<3" cursor.execute(sql_delete) print cursor.rowcount conn.commit() #如果没有commit则上述命令对数据库不会改变 cursor.close() conn.close()

运行结果

6 (1L, u'name1') ((2L, u'name1'), (3L, u'name3')) ((4L, u'name4'), (5L, u'name5'), (6L, u'name6')) 1 1 2

此时查看数据的变化

mysql> select * from account; +----+-------+ | id | name | +----+-------+ | 3 | name3 | | 4 | xuna | | 5 | name5 | | 6 | name6 | | 7 | name7 | +----+-------+ 5 rows in set (0.00 sec)

看到运行结果和数据库表的变化,对于python操作mysql数据库的流程也很清晰啦。

更新: 有时候你想一下子添加多组数据可以这样写:

mysql> insert into account values ( '1','name1'), ( '2','name2'), ( '3','name3'), ( '4','name4');

或者

sql_insert = "insert into account values(%d,%s)" cursor.executemany(sql_insert,[(1,name1),(7,name7),(2,name2),(3,name3)]) print cursor.rowcount
转载请注明原文地址: https://www.6miu.com/read-10108.html

最新回复(0)