1 Star 0 Fork 0

song-xiaohui/RushTicket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MysqlDB.py 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
songxiaohui 提交于 2018-12-05 20:44 . init
import pymysql.cursors
from entity.shop import Shop
def getConnection():
# Connect to the database
connection = pymysql.connect(host='111.231.232.212',
user='root',
password='mysqlroot',
db='rushticket',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return connection
# query All Data
def queryAll(querySql):
querySql = "select * from shop"
return execute(querySql)
# query data by condition
def queryByCondition(condition):
if not condition:
return None
queryByConditionSql = "select * from shop where id = " + str(condition)
return execute(queryByConditionSql)
# insert data by object
def insert(obj):
if not obj:
return None
try:
connect = getConnection()
with connect.cursor() as cursor:
sql = "insert into ticket(secrtkey, `number`, trainCode, isMore, secondClass, firstClass, zeroClass, `time`) values (%s,%s,%s,%s,%s,%s,%s,%s)"
res = cursor.execute(sql, (obj.secrtkey, obj.number, obj.trainCode, obj.isMore, obj.secondClass, obj.firstClass, obj.zeroClass, obj.time))
connect.commit()
return res
except Exception as err:
print(err)
connect.rollback()
finally:
connect.close()
def execute(sql):
if not sql:
return None
try:
connection = getConnection()
with connection.cursor() as cursor:
cursor.execute(sql)
# connection.commit()
# fetch 是抽取数据的
return cursor.fetchall()
finally:
connection.close()
if __name__ == '__main__':
shop = Shop("songyinghui2","ssss", 2.9, "python测试", 1.98, "/img/song.jpg", "1")
n = insert(shop)
# result = queryByCondition(1)
print("execute end")
print(n)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/song-xiaohui/RushTicket.git
git@gitee.com:song-xiaohui/RushTicket.git
song-xiaohui
RushTicket
RushTicket
master

搜索帮助