Python operation MySQL

Source: Internet
Author: User
1.python 2.7.2 (Installation version)
2.mysql-python-1.2.3.win32-py2.7 (MySQLdb module)

#连接数据库代码
#coding =utf-8
Import MySQLdb
Import Sys
Import time
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 ()
#列出所有数据库
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
#使用test数据库, query all tables below the test database
Cur.execute ("Use test")
Cur.execute ("Select Database ()")
Print "Current database:%s"%cur.fetchall () [0]
all_table = Cur.execute ("Show Tables")
For TB in Cur.fetchall ():
Tb_list.append (Tb[0])
Print Tb_list
#查询数据表userinfo
Userlist=[]
Cur.execute ("Use test")
Cur.execute ("select * from UserInfo;")
For Shuju in Cur.fetchall ():
Userlist.append (Shuju)
Print UserList
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.