1.使用python操作mysql,希望使用当前时间建立表名.但是格式化表名报错.
2.代码如下图:
def __init__(self):
time1 = datetime.datetime.now().strftime('%Y-%m-%d')
self._tabname = 'sound_' + time1
self.conn = MySQLdb.connect(host="localhost", user="root", passwd="ubuntu", db="spider", charset="utf8")
self.cursor = self.conn.cursor()
try:
sql = """CREATE TABLE %s (
id int PRIMARY KEY AUTO_INCREMENT, Title varchar (255), Albumtitle varchar(255),
SinglePlayCount VARCHAR(255), Albumscore VARCHAR(255), LikeCount VARCHAR (255),
CommentsCount VARCHAR (255), TotalPlayCounts VARCHAR (255), Duration VARCHAR (255),
CreatedTime VARCHAR (255), displayDiscountedPrice VARCHAR (255),
Nickname VARCHAR (255), category_title VARCHAR (255), trackId VARCHAR (255))
)"""
self.cursor.execute(sql, (self._tabname, ))
except:
pass
3.报错截图如下:
4.请教下.如何能格式化表名,让它为当前时间,尝试过format也不行.
5.麻烦提供方法,或者思路都可以.非常感谢!