python复制表1000个

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sqlite3
import os
import datetime


server_root_path = os.path.dirname(os.path.abspath(__file__))

#server_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

class ConnSql(object):
    def __init__(self, db_path=r'{}'.format(server_root_path), db_name='test'):
        self.db_path = '{}/{}.db'.format(db_path, db_name)
        print(server_root_path, self.db_path )
        self.cxn = None
        self.cur = None
    def __del__(self):
        print ('__del__ called')
    def conn_db(self):
        self.cxn = sqlite3.connect(self.db_path)
        self.cur = self.cxn.cursor()
        print('connect ok!')

    def close_db(self):
        if self.cxn and self.cur:
            self.cur.close()
            self.cxn.close()
            print('close ok!')

    def create_table(self, create_teble_sql):
        self.cur.execute(create_teble_sql)

    def search_info(self, search_sql):
        # get_info = self.cur.execute('''
        #         select * from users
        #     ''')
        get_info = self.cur.execute(search_sql)
        print(get_info)
        return get_info
    def update_info(self, update_sql):
        # self.cur.execute('''
        #         insert into users (name) values ('test_name1')
        #     ''')
        self.cur.execute(update_sql)
        self.cxn.commit()
        print('update info ok!')






def sql_main():
    sql_obj = ConnSql(db_name="auxdb100w")
    sql_obj.conn_db()

    in_date = '2015-01-01'
    for i in range(1,1000):
	dt = datetime.datetime.strptime(in_date, "%Y-%m-%d")
	out_date = (dt + datetime.timedelta(days=i)).strftime("%Y%m%d")
	print(out_date)
	sql='''insert into t_date (directory) values ('%s')''' % (out_date)
        print sql
        sql_obj.update_info(update_sql=sql)
        sql='''create table t_%s as select * from t_x''' % (out_date)
        print sql
        info = sql_obj.search_info(search_sql=sql)


    sql_obj.close_db()
    del sql_obj

if __name__ == '__main__':
    sql_main()




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值