mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-11-30 19:08:48 +08:00
sqlite内存数据库实时缓存
This commit is contained in:
parent
af6ed1153f
commit
916d596d9c
60
hikyuu/data/SqliteMemDriver.py
Normal file
60
hikyuu/data/SqliteMemDriver.py
Normal file
@ -0,0 +1,60 @@
|
||||
# -*- coding: utf8 -*-
|
||||
#
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2010-2019 fasiondog/hikyuu
|
||||
#
|
||||
|
||||
import os
|
||||
import sys
|
||||
import sqlite3
|
||||
|
||||
from hikyuu.util import *
|
||||
|
||||
|
||||
def dbcommit(func):
|
||||
def wrapfunc(*args, **kwargs):
|
||||
result = None
|
||||
x = args[0]
|
||||
try:
|
||||
hku_check(x.cursor is None, "You cannot nest using database cursors!")
|
||||
x.cursor = x._get_cursor()
|
||||
result = func(*args, **kwargs)
|
||||
x._cnx.commit()
|
||||
except Exception as e:
|
||||
hku_error("{} [{}.{}]".format(e, func.__module__, func.__name__))
|
||||
result = None
|
||||
|
||||
if x.cursor is not None:
|
||||
x.cursor.close()
|
||||
x.cursor = None
|
||||
return result
|
||||
|
||||
return wrapfunc
|
||||
|
||||
|
||||
class SqliteMemDriver:
|
||||
def __init__(self):
|
||||
self._cnx = sqlite3.connect("file:hikyuu_mem?mode=memory&cache=shared")
|
||||
self.cursor = None
|
||||
|
||||
def _get_cursor(self):
|
||||
return self._cnx.cursor()
|
||||
|
||||
@dbcommit
|
||||
def create_table(self):
|
||||
filename = os.path.dirname(__file__) + '/sqlite_mem/createdb.sql'
|
||||
with open(filename, 'r', encoding='utf8') as f:
|
||||
self.cursor.executescript(f.read())
|
||||
|
||||
@dbcommit
|
||||
def insert_bars(self, market, code, bars, ktype):
|
||||
kline = [
|
||||
(
|
||||
code, bar['datetime'], bar['open'], bar['high'], bar['low'], bar['close'],
|
||||
bar['amount'], bar['volumn']
|
||||
) for bar in bars
|
||||
]
|
||||
sql = 'INSERT OR REPLACE INTO {}_{}(code, datetime, open, high, low, close, amount, \
|
||||
volumn) VALUES (?,?,?,?,?,?,?,?)'.format(market, ktype)
|
||||
self.cursor.executemany(sql, kline)
|
1
hikyuu/data/sqlite_mem/__init__.py
Normal file
1
hikyuu/data/sqlite_mem/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
__version__ = "0.0.1"
|
46
hikyuu/data/sqlite_mem/createdb.sql
Normal file
46
hikyuu/data/sqlite_mem/createdb.sql
Normal file
@ -0,0 +1,46 @@
|
||||
BEGIN TRANSACTION;
|
||||
CREATE TABLE "sh_day" (
|
||||
"code" TEXT NOT NULL,
|
||||
"datetime" NUMERIC NOT NULL,
|
||||
"open" REAL NOT NULL,
|
||||
"high" REAL NOT NULL,
|
||||
"low" REAL NOT NULL,
|
||||
"close" REAL NOT NULL,
|
||||
"amount" REAL NOT NULL,
|
||||
"volumn" REAL NOT NULL,
|
||||
PRIMARY KEY("code","datetime")
|
||||
);
|
||||
CREATE TABLE "sz_day" (
|
||||
"code" TEXT NOT NULL,
|
||||
"datetime" NUMERIC NOT NULL,
|
||||
"open" REAL NOT NULL,
|
||||
"high" REAL NOT NULL,
|
||||
"low" REAL NOT NULL,
|
||||
"close" REAL NOT NULL,
|
||||
"amount" REAL NOT NULL,
|
||||
"volumn" REAL NOT NULL,
|
||||
PRIMARY KEY("code","datetime")
|
||||
);
|
||||
CREATE TABLE "sh_min" (
|
||||
"code" TEXT NOT NULL,
|
||||
"datetime" NUMERIC NOT NULL,
|
||||
"open" REAL NOT NULL,
|
||||
"high" REAL NOT NULL,
|
||||
"low" REAL NOT NULL,
|
||||
"close" REAL NOT NULL,
|
||||
"amount" REAL NOT NULL,
|
||||
"volumn" REAL NOT NULL,
|
||||
PRIMARY KEY("code","datetime")
|
||||
);
|
||||
CREATE TABLE "sz_min" (
|
||||
"code" TEXT NOT NULL,
|
||||
"datetime" NUMERIC NOT NULL,
|
||||
"open" REAL NOT NULL,
|
||||
"high" REAL NOT NULL,
|
||||
"low" REAL NOT NULL,
|
||||
"close" REAL NOT NULL,
|
||||
"amount" REAL NOT NULL,
|
||||
"volumn" REAL NOT NULL,
|
||||
PRIMARY KEY("code","datetime")
|
||||
);
|
||||
COMMIT;
|
Loading…
Reference in New Issue
Block a user