jqkyp 发表于 2018-8-9 10:31:24

python获取mysql表信息

#!/bin/env python  #coding=utf-8
  #by songry
  #date 2018-01-09
  #time 11:18
  #power database
  import pymysql
  # 打开数据库连接(ip/数据库用户名、登录密码、数据库名)
  db = pymysql.connect("ipxx","userroot","passwd","dbname")
  #使用 cursor() 方法创建一个游标对象 cursor
  cursor = db.cursor()
  ##############################################获取单条数据#####
  #使用execute()方法执行sql查询
  cursor.execute("SELECT aaa.* from xxx")
  #使用fetchone()方法获取单条数据
  data = cursor.fetchone()
  print data
  ##目前只能获取单条数据
  ############################# 若多条数据 ############
  #使用execute()方法执行sql查询
  #获取表中有多少数据
  aa=cursor.execute("SELECT aaa.ip_address from xx;")
  print aa
  #打印表中的多少数据
  info=cursor.fetchmany(aa)
  for ii in info:
  print ii
  ##########################################################
  #关闭游标
  #cursor.close()
  #提交数据,增删等操作,若不提交,数据不会真的变更
  #db.commit()
  #关闭数据库
  db.close()
  #############
页: [1]
查看完整版本: python获取mysql表信息