MySQL数据库 数据按照时间每天一张数据表

import java.sql.Date;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cetcnav.suo.ADSB.util.DateUtil;

@Repository("createSpeedTable")
public class CreateSpeedTableDao {
	
	private static final Logger logger = LoggerFactory.getLogger(CreateSpeedTableDao.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public void createSpeedTable(Date dateParam) {
		
		logger.info("开始转存位置数据(createspeedTable)>>...");
		String sql = "SELECT distinct date_format(time,'%Y-%m-%d') as date FROM speed";
		List<Date> dates = jdbcTemplate.queryForList(sql,Date.class);
		
		Date today = new Date(DateUtil.getCurrentYYR().getTime());
		if(dates == null || dates.size() <= 0){
			String time = String.valueOf(dateParam);  //2016-04-01
			String table = "speed_"+time.replace("-", "");  //speed_monitor_20160401
			String cSql = "create table IF not exists "+table+"  (like speed)";	// INCLUDING DEFAULTS
			jdbcTemplate.execute(cSql);
			return;
		}
		
		for(Date date : dates){
			logger.info(date.toString()+" : "+today.toString());
			Date dataIndex = new Date(DateUtil.convertDate2FirstSecond(date).getTime());
			if(dataIndex.equals(today) || dataIndex.after(today))
				continue;
//			logger.info("需要转存的日期"+date.toString());
			
			String time = String.valueOf(date);  //2016-04-01
			Calendar c = Calendar.getInstance();
			c.setTime(date); 
			int day = c.get(Calendar.DATE);
			c.set(Calendar.DATE, day+1);
			Date newDate = new java.sql.Date(c.getTime().getTime());
			String table = "speed_"+time.replace("-", "");  //speed_monitor_20160401
			String cSql = "create table IF not exists "+table+"  (like speed )";	
			jdbcTemplate.execute(cSql);
			String query = "select * from speed where time >= '"+String.valueOf(date)+" 00:00:00.0' and time < '"+newDate+"'";
			String inSql = "insert into "+table+" "+query+";";
			jdbcTemplate.execute(inSql);
			String deleteSql = "delete from speed where id in (select id from "+table+")";
			jdbcTemplate.execute(deleteSql);
		}
	}
	

}

可以通过在Python中使用MySQL Connector来连接到MySQL数据库并按照时间序列读取每张表的数据。以下是一个示例代码: ```python import mysql.connector from mysql.connector import Error # 连接到MySQL数据库 try: connection = mysql.connector.connect(host='localhost', database='mydatabase', user='myusername', password='mypassword') if connection.is_connected(): db_Info = connection.get_server_info() print("连接到 MySQL 数据库版本:", db_Info) # 获取数据库中所有表的名称和创建时间 cursor = connection.cursor() cursor.execute("SHOW TABLES") tables = cursor.fetchall() table_info = {} for table in tables: cursor.execute(f"SHOW CREATE TABLE {table[0]}") create_table = cursor.fetchone()[1] table_info[table[0]] = create_table.split('\n')[1].strip() # 按照创建时间排序表名 sorted_tables = sorted(table_info.items(), key=lambda x: x[1]) # 读取每张表的数据 for table in sorted_tables: cursor.execute(f"SELECT * FROM {table[0]}") rows = cursor.fetchall() for row in rows: print(row) except Error as e: print("连接失败:", e) finally: # 关闭数据库连接 if (connection.is_connected()): cursor.close() connection.close() print("连接已关闭") ``` 该代码首先连接到MySQL数据库,然后获取数据库中所有表的名称和创建时间。接下来,它按照创建时间排序表名,并依次读取每张表的数据。最后,关闭数据库连接。 请注意,该代码仅为示例代码,实际应用中可能需要根据具体情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值