중복된 데이터들을 찾아서 제거하려고 SQL 쿼리문을 만들었는데 실행이 안돼서 설마 하는 마음에 구글링을 해봤더니 칼럼명에 "index"를 포함한 어떤 특정 단어를 쓰면 안 된다는 걸 깨달았다.
https://www.sqlite.org/lang_keywords.html
SQLite Keywords
The SQL standard specifies a large number of keywords which may not be used as the names of tables, indices, columns, databases, user-defined functions, collations, virtual table modules, or any other named object. The list of keywords is so long that few
www.sqlite.org
분봉이나 일봉 모두 시계열정보는 index라는 컬럼에다가 넣어놨었는데 (왜 그랬니?) 모든 데이터베이스의 모든 테이블의 해당 컬럼명을 전부 바꿔야 한다. 데이터베이스를 사용하는 다른 프로그램들도 죄다 바꾸어야 하는데 일단 데이터베이스파일들의 칼럼명을 바꾸는 코드만 우선적으로 만들어서 적용했다. sqlite_master에 접근해서 모든 테이블명을 가지고 온 후에 하나씩 순회하면서 ALTER TABLE RENAME 명령을 써서 지정된 칼럼명을 새롭게 바꾸어주는 코드를 만들었다.
import sqlite3
con = sqlite3.connect('database.db')
cur = con.cursor()
# 지정된 데이터베이스 파일 내에 존재하는 모든 테이블명을 리스트에 변환
cur.execute("SELECT name FROM sqlite_master WHERE type='table';")
table_list = [item[0] for item in cur.fetchall()]
print(table_list)
# 각 테이블마다 index컬럼명을 idx로 바꿈
for table_name in table_list:
old_name = 'index'
new_name = 'idx'
sql = f'ALTER TABLE "{table_name}" RENAME COLUMN "{old_name}" TO "{new_name}"'
cur.execute(sql)
'Python, API' 카테고리의 다른 글
[Python] Sharp Ratio / Sortino Ratio / Carmar Ratio 계산 (0) | 2023.06.19 |
---|---|
[Python] 체결데이터로 분봉(OHLCV) 만들기 (0) | 2023.06.04 |
Numba 0.57 Release (Support for Python 3.11) (0) | 2023.05.03 |
[Python/키움API] 주식 틱차트 조회요청 (OPT10079) 및 저장 (2) | 2023.04.24 |
[Python] DataFrame에 필터 적용 후 여러 개로 쪼개기 (0) | 2023.03.26 |