1.安装mysql
sudo apt-get install mysql-server
Sudo apt-get install mysql-client
2.安装MySQL-python驱动
sudo apt-get install mysql-python
3.测试是否安装成功
python解释器命令下运行 import MySQLdb
没有报错,说明安装已成功。
4.python使用mysql数据库常见操作
#-*- coding: utf-8 -*-
importMySQLdbtry:#mysql连接
conn = MySQLdb.connect(host='localhost',user='root',passwd='password',port=3306,charset='utf8')#创建游标
cur =conn.cursor()#创建数据库
cur.execute('create database if not exists pythondb')#切换到数据库
conn.select_db('pythondb')#创建表
cur.execute("create table student(id int,name varchar(20),age int)")#插入单条数据
cur.execute("insert into student values(9,'张三',28)")#插入多条数据
cur.execute("insert into student values(2,'shijingjing08',21),(3,'shijingjing