MySQL Connector/Python——操作mysql

Python MySQL 数据操作
本文介绍如何使用 Python 的 MySQL Connector 库来实现与 MySQL 数据库的交互,包括连接数据库、创建表、插入数据、查询数据等基本操作,并提供了一个 Django 中的配置示例。

———————————————python3.3_connector1.1.4_windows—————————————————————

首先在http://dev.mysql.com/downloads/connector/python/下载connector/python,然后直接点击安装包进行安装,如果安装python的时候选择的是只为自己安装这个选项,那么就会出现在安装connector/python的时候提示说找不到python3.3...

 

 

1、连接mysql

 

ExpandedBlockStart.gif
import mysql.connector
cnx=mysql.connector.connect(user= ' root ',password= '',host= ' 127.0.0.1 ',database= ' lazystudy ')
cnx.close()
View Code

 

 

 

2、创建表和创建数据库

ExpandedBlockStart.gif
from mysql.connector  import errorcode
import mysql.connector
from symbol  import except_clause

DB_NAME= ' lazystudy '
TABLES={}

TABLES[ ' Book ']= """
    create table book(
        id int(11) auto_increment primary key,
        bookname varchar(20) not null,
        price int(11) not null
    )
"""

TABLES[ ' People ']= """
    create table people(
        id int(11) auto_increment primary key,
        name varchar(20) not null
    )
"""

cnx=mysql.connector.connect(user= ' root ',password= '',host= ' 127.0.0.1 ')

cursor=cnx.cursor()

def create_database(cursor):
     try:
        cursor.execute( " create database {} default character set 'utf-8' ".format(DB_NAME))
     except mysql.connector.Error as err:
         print( ' failed creating database:{} '.format(DB_NAME))
        exit(1)

try:
    cnx.database=DB_NAME
except mysql.connector.Error as err:
     if(err.errno==errorcode.ER_BAD_DB_ERROR):
        create_database(cursor)
        cnx.database=DB_NAME
     else:
         print(err)
        exit(1)


for name,ddl  in TABLES.items():
     try:
         print( ' creating table {} '.format(name),end= '   ')
        cursor.execute(ddl)
     except mysql.connector.Error as err:
         if(err.errno==errorcode.ER_TABLE_EXISTS_ERROR):
             print( " already exits... ")
         else:
             print(err.msg)
     else:
         print( " successfully... ")


cursor.close()
cnx.close()
View Code

 

3、插入数据

ExpandedBlockStart.gif
"""
insert into db
"""
import mysql.connector
cnx=mysql.connector.connect(user= ' root ',password= '',database= ' lazystudy ',host= ' 127.0.0.1 ')
cursor=cnx.cursor()

add_book= """
    insert into book (bookname,price) values(%s,%s)
"""
data_book=( ' the one ',100)

add_people= """
    insert into people (name) values(%s)
"""
data_people=( ' lazy ',) # 记得加上','  ——不然会出错...这是python元组的特性..。

# insert...
cursor.execute(add_book,data_book)
cursor.execute(add_people,data_people)

# close
cnx.commit()
cursor.close()
cnx.close()
View Code

 

 4、查询数据

ExpandedBlockStart.gif
import mysql.connector
cnx=mysql.connector.connect(user= ' root ',password= '',database= ' lazystudy ')
cursor=cnx.cursor()
query= """
    select * from people where id between %s and %s
"""
query_limit=(1,3)
cursor.execute(query,query_limit)

for info  in cursor:
     print( " name:{} ".format(info[1]))

cursor.close()
cnx.close()
View Code

 

 5、在django中的配置

ExpandedBlockStart.gif
DATABASES = {
     ' default ': {
         ' NAME '' lazystudy ',
         ' ENGINE '' mysql.connector.django ',
         ' USER '' root ',
         ' PASSWORD ''',
         ' OPTIONS ': {
           ' autocommit ': True,
        },
    }
}
View Code

 

 

 

 

转载于:https://www.cnblogs.com/lazyzhong/p/3489693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值