web.py mysql_insert.py

"""
web.py mysql_insert.py
参考:http://webpy.org/cookbook/Insert.zh-cn
功能:mysql insert
测试URL:
http://localhost:8080/hello
http://127.0.0.1:8080/hello
http://127.0.0.1:8080/mysql/insert

服务器响应:
Hello, world!
成功。
"""
import web

# 建立数据库连接。
db = web.database(dbn='mysql', db='test', user='root', pw='123456')
# db = web.database(dbn="mysql", host="localhost", port=3306, db="test", user="root", pw="123456")
urls = (
    "/hello", "Hello",
    "/mysql/insert", "MysqlInsert",
)


class Hello:
    def GET(self):
        """
        此方法,必须是大写。
        如果改成get,则报错:"HTTP/1.1 GET /" - 405 Method Not Allowed。
        :return:
        """
        return 'hello, world!'


class MysqlInsert:
    def GET(self):
        table = "customers"
        # INSERT INTO customers (address, age, id, name, salary) VALUES ('Tianjin', 31, 10, 'Tom', 8000.0)
        sequence_id = db.insert(table, id=10, name='Tom', age=31, address='Tianjin', salary=8000.00)
        print("sequence_id:", sequence_id)
        return 'inserted.'


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()
发布了198 篇原创文章 · 获赞 58 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/103868745