win7上python2.7连接mysql数据库的方法

yipeiwu_com7年前Python基础

 一:安装MySQL-python驱动

 pip install mysql

这里写图片描述

二:连接到MySQL服务器的test数据库

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector
if __name__ == "__main__":
   # 打开数据库连接
  conn=mysql.connector.connect(host='IP',
       user='root',
       passwd='1234',
       db='dbName')

  # 使用cursor()方法获取操作游标
  cursor=conn.cursor()

  # 使用execute方法执行SQL语句
  cursor.execute('select * from tset where 1=1') #表查询

  # 使用 fetchone() 方法获取一条数据库。
  values=cursor.fetchall()

  print(values)

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

这里写图片描述

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3序列化与反序列化用法实例

本文实例讲述了python3序列化与反序列化用法。分享给大家供大家参考。具体如下: #coding=utf-8 import pickle aa={} aa["title"]="我是...

python aiohttp的使用详解

python aiohttp的使用详解

1.aiohttp的简单使用(配合asyncio模块) import asyncio,aiohttp async def fetch_async(url): print(url)...

Python Tkinter GUI编程入门介绍

Python Tkinter GUI编程入门介绍

一、Tkinter介绍 Tkinter是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口。Tkinter不是唯一的python图形编程接口,但是是其中...

python多进程共享变量

本文实例为大家分享了python多进程共享变量的相关代码,供大家参考,具体内容如下 from multiprocessing import Process, Manager impo...

Python中dict和set的用法讲解

Python中dict和set的用法讲解

dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 举个例子,假设要...