How to add, modify, delete, and delete MySQL strings using python

Source: Internet
Author: User
This article introduces how to use python to add, modify, delete, and operate on MySQL strings. This article introduces how to use python to add, modify, delete, and operate on MySQL strings.

# Coding = UTF-8import MySQLdbdef dbDperate (SQL, param): "Define database addition, modification and deletion operations" # get database connection object conn = MySQLdb. connect (host = "localhost", user = "root", passwd = "root", db = "tester", charset = "utf8 ") # obtain the executed cursor object cursor = conn. cursor () # run the SQL statement cursor.exe cute (SQL, param) # submit the MySql transaction conn. commit () cursor. close () conn. close () # Add a piece of data # addSql = "insert into news_user (username, password) values (% s, % s)" # addParam = ("Wang Zheng ", "wangzheng") # dbDperate (addSql, addParam) # modifying a piece of data # updateSql = "update news_user set username = % s where id = % s" # updateParam = ("Wang Zheng new", str (1 )) # dbDperate (updateSql, updateParam) # delete a piece of data # deleteSql = "delete from news_user where id = % s" # deleteParam = (str (1) # dbDperate (deleteSql, deleteParam)
# Coding = UTF-8import json # json string json1 = "{\" name \ ": \" Wang Zheng \ ", \" age \": 21} "# Convert a json string into a Python dictionary object pydict = json. loads (json1) print "pydict type:", type (pydict) print "name value in json ", pydict ["name"] print "age value in json", pydict ["age"]
# Coding = UTF-8import jsondict1 = {"name": "Wang Zheng", "age": 21} # Convert the dictionary object to a json string jsonStr = json. dumps (dict1, ensure_ascii = False) print jsonStr

We have two methods for software design: one is to make it simple enough to make bugs invisible, and the other is to make it complex enough to make people unable to find bugs. The former is more difficult.

The preceding section describes how to add, modify, delete, and operate strings in MySQL using python. For more information, see other related articles in the first PHP community!

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.