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()




你可能感兴趣的:(python)