>  기사  >  백엔드 개발  >  Python은 mysql을 운영합니다.

Python은 mysql을 운영합니다.

巴扎黑
巴扎黑원래의
2016-12-03 10:00:37833검색

1.python 2.7.2(설치 버전)
2.MySQL-python-1.2.3.win32-py2.7(mysqldb 모듈)

#데이터베이스 코드에 연결
# 코딩 =utf-8
import MySQLdb
import sys
가져오기 시간
host ='127.0.0.1'
user ='root'
passwd ='123456'
port = 3306
db_list=[]
tb_list=[]
dbcon=MySQLdb.connect(host,user,passwd,port=3306)
cur=dbcon.cursor()
#List 모든 데이터베이스
def check_db():
cur.execute('show Databases')
for db in cur.fetchall():
db_list.append(db[0])
return db_list
#현재 데이터베이스의 모든 테이블 목록 가져오기
db_list = check_db()
print db_list
#테스트 데이터베이스를 사용하여 테스트 데이터베이스 아래의 모든 테이블을 쿼리합니다.
cur.execute (" 테스트 사용")
cur.execute("select Database()")
print "현재 데이터베이스: %s" %cur.fetchall()[0]
all_table = cur.execute(" show tables ")
for tb in cur.fetchall():
tb_list.append(tb[0])
print tb_list
#Query data table userinfo
userList=[]
cur .execute("테스트 사용")
cur.execute("userinfo에서 * 선택;")
cur.fetchall()의 shuju에 대해:
userList.append(shuju)
인쇄 사용자 목록

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.