Python accesses the MySQL database and the pythonmysql database.

Source: Internet
Author: User

Python accesses the MySQL database and the pythonmysql database.

# Encoding: utf-8import mysql. connector _ author _ = 'admin' config = {'host': '2017. 0.0.1 ', # default 127.0.0.1 'user': 'root', 'Password': 'root', 'Port': 3306, # default value: 3306 'database ': 'test', 'charset': 'utf8' # The default value is utf8} if _ name __= = "_ main __": print (config ['host']) cnx = mysql. connector. connect (user = config ['user'], password = config ['Password'], host = config ['host'], port = config ['Port'], database = config ['database']) cursor = cnx. cursor () cursor.exe cute ("select * from job") for _ row in cursor. fetchall (): print (_ row [1]) pass

 


How to Use python to import SQL

MySQLdb Module
Import OS, sys
Import MySQLdb
Try:
Conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = "", db = 'yourdb ')
Except t Exception, e:
Print e
Sys. exit ()
Cursor = conn. cursor ()
SQL = "insert into address (name, address) values (% s, % s )"
Values = ("zhang", "being"), ("li", "beijing"), ("wang", "beijing "))
Try:
Cursor.exe cute.pdf (SQL, values) # insert several data records
Except t Exception, e:
Print e
SQL = "select * from address"
Cursor.exe cute (SQL) # Query
Data = cursor. fetchall ()
If data:
For x in data:
Print x [0], x [1]
Cursor. close () # close the cursor
Conn. close () # close the database

In linux, how does one view Chinese Characters in python when python connects to the MYSQL database?

Are you using print output to find garbled characters from the database?

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.