Python connection MySQL database query operation with a WHERE condition

Source: Internet
Author: User

 #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

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.