add timestamp field to mysql
This commit is contained in:
parent
1f20355d9d
commit
2903506991
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -161,3 +161,5 @@ cython_debug/
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
# My File
|
||||||
|
.secret
|
2
.secret
2
.secret
|
@ -1 +1 @@
|
||||||
o36r0lgw71mdzm9rkwrv3wi1wn|3600|1715909100.492765|oadugxyl9fhoqsamqopc
|
ontipj0py431xul5zm4kj6gzzo|3600|1715933457.543795|oadugxyl9fhoqsamqopc
|
|
@ -24,7 +24,9 @@ class DecspiderPipeline:
|
||||||
# 检查表是否存在,如果不存在就创建表
|
# 检查表是否存在,如果不存在就创建表
|
||||||
self.cursor.execute(f"""
|
self.cursor.execute(f"""
|
||||||
CREATE TABLE IF NOT EXISTS `{self.table_name}` (
|
CREATE TABLE IF NOT EXISTS `{self.table_name}` (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
@ -35,21 +37,31 @@ class DecspiderPipeline:
|
||||||
# 获取 NewsItem 字段信息
|
# 获取 NewsItem 字段信息
|
||||||
item_columns = set(NewsItem.fields.keys())
|
item_columns = set(NewsItem.fields.keys())
|
||||||
|
|
||||||
# 添加 NewsItem 字段到表中
|
# 添加 NewsItem 字段到表中
|
||||||
for column in item_columns:
|
for column in item_columns:
|
||||||
if column not in existing_columns:
|
if column not in existing_columns:
|
||||||
self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `{column}` TEXT")
|
self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `{column}` TEXT")
|
||||||
spider.log(f'Added column `{column}` to `{self.table_name}` table')
|
spider.log(f'Added column `{column}` to `{self.table_name}` table')
|
||||||
|
|
||||||
|
# 添加 created_at 和 updated_at 字段,如果它们不存在
|
||||||
|
if 'created_at' not in existing_columns:
|
||||||
|
self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
|
||||||
|
spider.log(f'Added column `created_at` to `{self.table_name}` table')
|
||||||
|
|
||||||
|
if 'updated_at' not in existing_columns:
|
||||||
|
self.cursor.execute(f"ALTER TABLE `{self.table_name}` ADD COLUMN `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
|
||||||
|
spider.log(f'Added column `updated_at` to `{self.table_name}` table')
|
||||||
|
|
||||||
# 删除表中不存在于 NewsItem 中的字段
|
# 删除表中不存在于 NewsItem 中的字段
|
||||||
for column in existing_columns:
|
for column in existing_columns:
|
||||||
if column not in item_columns and column != 'id':
|
if column not in item_columns and column not in {'id', 'created_at', 'updated_at'}:
|
||||||
self.cursor.execute(f"ALTER TABLE `{self.table_name}` DROP COLUMN `{column}`")
|
self.cursor.execute(f"ALTER TABLE `{self.table_name}` DROP COLUMN `{column}`")
|
||||||
spider.log(f'Dropped column `{column}` from `{self.table_name}` table')
|
spider.log(f'Dropped column `{column}` from `{self.table_name}` table')
|
||||||
|
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def close_spider(self, spider):
|
def close_spider(self, spider):
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user