Python for mysql

Source: Internet
Author: User
1. python2.7.2 (installation version) 2. MySQL-python-1.2.3.win32-py2.7 (mysqldb module) # connection database code # codingutf-8importMySQLdbimportsysimporttimehost127.0.0.1userrootpasswd123456port3306db_list [] tb .. 1. python 2.7.2 (installation version)
MySQL-python-1.2.3.win32-py2.7 (mysqldb module)

# Database Connection code
# Coding = UTF-8
Import MySQLdb
Import sys
Import time
Host = '2017. 0.0.1'
User = 'root'
Passwd = '000000'
Port = 3306
Db_list = []
Tb_list = []
Dbcon = MySQLdb. connect (host, user, passwd, port = 3306)
Cur = dbcon. cursor ()
# List all databases
Def check_db ():
Cur.exe cute ('Show databases ')
For db in cur. fetchall ():
Db_list.append (db [0])
Return db_list
# Retrieve the list of all tables in the current database
Db_list = check_db ()
Print db_list
# Use the test database to query all tables in the test Database
Cur.exe cute ("use test ")
Cur.exe cute ("select database ()")
Print "current database: % s" % cur. fetchall () [0]
All_table = cur.exe cute ("show tables ")
For tb in cur. fetchall ():
Tb_list.append (tb [0])
Print tb_list
# Querying the data table userinfo
UserList = []
Cur.exe cute ("use test ")
Cur.exe cute ("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.