1
p2pCoder 2017-11-24 14:09:44 +08:00
写个存储过程?
本质上 都是 向三个表 分别插入记录,没啥区别 |
2
qiayue 2017-11-24 14:10:44 +08:00
或者简单点,用事务,然后三条 sql
|
3
shenhhd 2017-11-24 14:25:23 +08:00
建立一个视图,向视图里插入!
|
4
bb2018 OP @p2pCoder
sql = 'INSERT INTO `phome_ecms_news` (classid, title, newstime) VALUES (%s, %s, %s)'value = (item['infotypeid'], item['infotitle'], item['addtime']) sql = 'INSERT INTO `phome_ecms_news_data_1` (classid, newstext) VALUES (%s, %s)' value = (item['infotypeid'], item['infocontent']) sql = 'INSERT INTO `phome_ecms_news_index` (classid, newstime) VALUES (%s, %s)' value = (item['infotypeid'], iitem['addtime']) conn.execute(sql, value) 这样子? |
5
bb2018 OP @qiayue
sql = 'INSERT INTO `phome_ecms_news` (classid, title, newstime) VALUES (%s, %s, %s)'value = (item['infotypeid'], item['infotitle'], item['addtime']) sql = 'INSERT INTO `phome_ecms_news_data_1` (classid, newstext) VALUES (%s, %s)' value = (item['infotypeid'], item['infocontent']) sql = 'INSERT INTO `phome_ecms_news_index` (classid, newstime) VALUES (%s, %s)' value = (item['infotypeid'], iitem['addtime']) conn.execute(sql, value) |
7
p2pCoder 2017-11-24 14:35:01 +08:00
@bb2018
try: cursor.execute(insertSQL1) cursor.execute(insertSQL2) cursor.execute(insertSQL3) cursor.close() conn.commit() except Exception, e: cursor.close() conn.rollback() 这是事务的方式 |
8
bb2018 OP @p2pCoder
try: cursor.execute( 'INSERT INTO `phome_ecms_news` (classid, title, newstime) VALUES (%s, %s, %s)'value = (item['infotypeid'], item['infotitle'], item['addtime'])) cursor.execute('INSERT INTO `phome_ecms_news_data_1` (classid, newstext) VALUES (%s, %s)' value = (item['infotypeid'], item['infocontent']) ) cursor.execute('INSERT INTO `phome_ecms_news_index` (classid, newstime) VALUES (%s, %s)' value = (item['infotypeid'], iitem['addtime']) ) cursor.close() conn.commit() except Exception, e: cursor.close() conn.rollback() 这个样子差不多? |
11
bb2018 OP @p2pCoder
这样不成功。 我用下面的方式: sql = 'INSERT INTO `dede_archives` (typeid, title, pubdate, voteid, infourl, ) VALUES (%s, %s, %s, %s, %s)' value = (item['infotypeid'], item['infotitle'], item['addtime'], 0, item['infourl']) conn.execute(sql, value) sql = 'INSERT INTO `dede_addonarticle` (aid, body) VALUES (%s, %s)' value = (item['infotypeid'], item['infocontent'], ) conn.execute(sql, value) 可以插入数据库 但是那两个表各有一个 id 字段,不能设成自增的。 那空上 id 字段该怎么赋值呢? |
13
shenhhd 2017-11-26 21:25:57 +08:00
|