分页查询报错,没理解
来源:3-3 python查询mysql数据库
Aegon_0
2018-12-28
#代码如下
import MySQLdb
class MysqlSearch(object):
def __init__(self):
self.get_conn()
def get_conn(self):
try:
self.conn = MySQLdb.connect(
host='127.0.0.1',
user='root',
password='',
db='news',
port=3306,
charset='utf8'
)
except MySQLdb.Error as e:
print('Error: %s' % e)
def close_conn(self):
try:
if self.conn:
self.conn.close()
except MySQLdb.Error as e:
print('Error: %s' % e)
def get_all1(self,page,page_size):
offset=(page - 1) * page_size
# 准备sql
sql='select * from `news` where `types`= %s desc limit %d,%d;'
# 找到cursor
cursor=self.conn.cursor()
# 执行sql
cursor.execute(sql,('百家',0,1 ))
# print(cursor.description)
# 得到结果
# print(cursor.fetchall())
result=[dict(zip([k[0] for k in cursor.description],row))for row in cursor.fetchall()]
cursor.close()
self.close_conn()
return result
def main():
obj = MysqlSearch()
result=obj.get_all1(1,1)
for item in result:
print(item)
if __name__ == '__main__':
main()

1回答
-
NavCat
2018-12-30
MySQLdb的字符串格式化不是标准的python的字符串格式化,应当一直使用%s用于字符串格式化
'LIMIT %s, %s;'
00
Python操作三大主流数据库-MySQL+MongoDB+Redis
2024 学习 · 376 问题
相似问题