#encoding =utf-8
Import MySQLdb
Class Sjb_data ():
def __init__ (self):
Self.url = URL
def MySQL (self,sql):
db = MySQLdb.connect (host= ' IP address ', port = 3306,user=r ' user name ', passwd= ' password ', db = ' database name ', charset= ' UTF8 ')
cur = db.cursor ()
Cur.execute (SQL) #执行sql
#data = Cur.fetchone () # using The Fetchone () method gets a data
results = Cur.fetchall () #获取查询表内所有数据
for I in results:
Print I #分别打印表内每一条数据
Db.close () #关闭数据库
If __name__ = = "__main__":
T = sjb_data ()
MySQL = "select * from Qyswfx_nsrjbxx t where t.nsrsbh= '%s '"% (value of '%s ')
T.mysql (MySQL)
#刚开始sql是mysql = "SELECT * from Qyswfx_nsrjbxx t where t.nsrsbh=%s"% (value of '%s '),
#查询一直报错 : _mysql_exceptions. Operationalerror: (1054, "Unknown column ' query condition value ' in ' WHERE clause '",
#在网上查了之后发现是因为where条件后面的值格式化输出的时候还是要加引号才能识别, the addition of the normal can be queried.
Python connection MySQL database query operation with a WHERE condition