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!