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?